
两者都可以分离容器.
那么docker exec和docker attach之间的真正区别是什么?
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 07002, we can use only one instance of shell.
So if we want open new terminal with new instance of container’s shell, we just need run 07003if the docker container was started using
/bin/bashcommand, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container usingexec.
如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
attachis not well named, particularly because of the LXC commandlxc-attach(which is more akindocker 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/bashwill give you a shell, but attaching to redis-server will be like you’d just started redis directly without daemonizing.
转载注明原文:docker attach和docker exec之间的区别 - 乐贴网