uncategorized

Docker exec和attach的区别

  1. exec 可以开启多个终端实例, exec -i /bin/bash,由此可见exec其实是在运行中的容器中执行一个命令,比如/bin/bash 来达到交互的目的。

The docker exec command runs a new command in a running container

  1. attach 开启一个和正在运行的进程交互的终端,如果该进程结束,原docker container的进程也会结束。attach只可以用在以 /bin/bash 命令启动的容器, 比如 docker run ubuntu /bin/bash

The docker attach command allows you to attach to a running container using the container’s ID or name, either to view its ongoing output or to control it interactively.

You can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of your detached process.

To stop a container, use CTRL-c. This key sequence sends SIGKILL to the container. If –sig-proxy is true (the default),CTRL-c sends a SIGINT to the container. You can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.

因此实质上两个命令不是同一个东西,只是对于以 /bin/bash 命令启动的容器,二者可以达到相同的效果

http://stackoverflow.com/questions/30960686/difference-between-docker-attach-and-docker-exec
http://askubuntu.com/questions/505506/how-to-get-bash-or-ssh-into-a-running-container-in-background-mode/507009#507009

(docker >= 1.3) If we use docker attach, 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 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.