gpt4 book ai didi

docker - docker attach 和 docker exec 的区别

转载 作者:IT老高 更新时间:2023-10-28 12:34:05 24 4
gpt4 key购买 nike

两者都可以在容器中执行命令。两者都可以分离容器。

那么 docker exec 和 docker attach 的真正区别是什么?

最佳答案

2015:有一个 commit PR添加到文档中:

Note: This command (attach) is not for running a new process in a container.See: docker exec.

Docker. How to get bash\ssh inside runned container ( run -d )?”的答案说明了区别:

(docker >= 1.3) If we use docker attach, we can use only one instance of shell.
So if we want to open new terminal with new instance of container's shell, we just need to run docker exec

if the docker container was started using /bin/bash command, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container using exec.

this issue 中所述:

  • Attach isn't for running an extra thing in a container, it's for attaching to the running process.
  • "docker exec" is specifically for running new things in a already started container, be it a shell or some other process.

同样的问题补充道:

While attach is not well named, particularly because of the LXC command lxc-attach (which is more akin docker exec <container> /bin/sh, but LXC specific), it does have a specific purpose of literally attaching you to the process Docker started.
Depending on what the process is the behavior may be different, for instance attaching to /bin/bash will give you a shell, but attaching to redis-server will be like you'd just started redis directly without daemonizing.


2022 年更新:使用来自 Containers 101: attach vs. exec - what's the difference? 的“ Ivan Velichko ”(2021 年 12 月)查看更多信息:

https://iximiuz.com/containers-101-attach-vs-exec/docker-attach-2000-opt.png

摘录:

Difference between attach and logs

On the diagram above, docker attach streams the container's logs back to the terminal.
However, the docker logs command does a similar thing.
So, what's the difference?

The logs command provides various options to filter the logs while attach in that regard acts as a simple tail.
But what's even more important is that the stream established by the logs command is always unidirectional and connected to the container's logs, not the container's stdio streams directly.

The logs command simply streams the content of the container's logs back to your terminal, and that's it.
So, regardless of how you created your container (interactive or non-interactive, controlled by a pseudo-terminal or not), you cannot accidentally impact the container while using the logs command.

However, when attach is used:

What does exec command do

The exec command is actually a totally different story.

In the case of attach, we were connecting our terminal to an existing container (read, process).

However, the exec command starts a totally new container!
In other words, exec is a form of the run command (which itself is just a shortcut for create + start).

Bart提醒我们the comments docker exec running 容器中运行新命令。不是“全新的”。

关于docker - docker attach 和 docker exec 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com