Both will be able to execute commands in container. Both could detach the container.
So what is the real difference between the docker exec and docker attach commands?
Both will be able to execute commands in container. Both could detach the container.
So what is the real difference between the docker exec and docker attach commands?
On
When a container is started using /bin/bash then it becomes the containers PID 1 and docker attach is used to get inside PID 1 of a container. So docker attach < container-id > will take you inside the bash terminal as it's PID 1 as we mentioned while starting the container. Exiting out from the container will stop the container.
Whereas in docker exec command you can specify which shell you want to enter into. It will not take you to PID 1 of the container. It will create a new process for bash. docker exec -it < container-id > bash. Exiting out from the container will not stop the container.
You can also use nsenter to enter inside containers. nsenter -m -u -n -p -i -t < pid of container > You can find PID of container using: docker inspect < container-id > | grep PID
Note: If you have started your container with -d flag then exiting out of container will not stop the container,whether you use attach or exec to get inside.
On
Docker exec executes a new command / create a new process in the container’s environment, while docker attach just connects the standard input/output/error of the main process(with PID 1) inside the container to corresponding standard input/output/error of current terminal(the terminal you are using to run the command).
A container is an isolated environment, with some processes running in the environment. Specifically, a container has its own file system space and PID space that are isolated from host and other containers. When the container is started using “docker run –it …”, the main process will have a pseudo-tty and STDIN kept open. When attached in the tty mode, you can detach from the container (and leave it running) using a configurable key sequence. The default sequence is CTRL-p CTRL-q. You configure the key sequence using the --detach-keys option or a configuration file. You can reattach to a detached container with docker attach.
Docker exec just starts a new process, inside the container’s environment, that is, belongs to the PID space of the container.
For example, if you start your container using “docker run –dit XXX /bin/bash”,you can attach to the container(‘s main process) using two different terminals. While you are inputting in one terminal, you can see it appears in the other terminal, for both terminal are connected to same tty. Be careful that you are now in the main process of the container, if you type “exit”, you will exit the container(so be careful, using detach-keys to detach), and you will see both terminals exited. But if you run “docker exec –it XXX /bin/bash” in two terminals, you have started two new processes inside the container, and they are not related to each other and to the main process, and you can safely exit from them.
On
As Michael Sun stated in his answer
docker execexecutes a new command / create a new process in the container’s environment, whiledocker attachjust connects the standard input/output/error of the main process(with PID 1) inside the container to corresponding standard input/output/error of current terminal(the terminal you are using to run the command).
My answer will focus more on letting you validate the above statement and understand it more clearly.
Open up a terminal window and run the command docker run -itd --name busybox busybox /bin/sh. The command will pull the image busybox if not already present. It will then create a container with the name busybox using this image.
You can check the status of your container by running the command docker ps -a | grep busybox.
If you run docker top busybox, you should see an output something like this.
UID PID PPID C STIME TTY TIME CMD
root 7469 7451 0 11:40 pts/0 00:00:00 /bin/sh
Of course, the PID, PPID and other values will be different in your case. You can use other tools and utilities as well like pstree, top, htop to see the list of PID and PPID.
The PID and PPID means the process id and parent process id. The process started when we created and started our container with the command /bin/sh. Now, run the command docker attach busybox. This will attach the standard input/output/error stream of the container to your terminal.
After attaching the container, create a shell session by running the command sh. Press CTRL-p CTRL-q sequence. This will detach the terminal from the container and will keep the container running. If you'll now run docker top busybox, you should see two processes in the list.
UID PID PPID C STIME TTY TIME CMD
root 7469 7451 0 11:40 pts/0 00:00:00 /bin/sh
root 7737 7469 0 11:43 pts/0 00:00:00 sh
But the PPID of the two processes will be different. In fact, the PPID of the second process will be the same as PID of the first one. The first process acts as the parent process for the shell session that we just created.
Now, run docker exec -it busybox sh. Once inside the container, check the list of running processes for the container busybox in another terminal window by running the command docker top busybox. You should see something like this
UID PID PPID C STIME TTY TIME CMD
root 7469 7451 0 11:40 pts/0 00:00:00 /bin/sh
root 7737 7469 0 11:43 pts/0 00:00:00 sh
root 7880 7451 0 11:45 pts/1 00:00:00 sh
The PPID of the first and third process will be the same, which confirms that docker exec creates a new process in the container's environment while docker attach just connects the standard input/output/error of the main process inside the container to corresponding standard input/output/error of current terminal.
On
To complement the valuable answer that currently indicates:
we can use only one instance of shell
And in the following post (which is mentioned in a comment in the valid answer)
indicates:
You can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of your detached process
At a first glance it seems a contradiction between them but after to did do some experiments both are correct according the perspective:
If in the host where docker is running was executed the docker run --name <containername> -itd ubuntu command and then:
tty and is executed for each one the docker attach <containername> command then these two tty share the same output according if any of them is used - even for typing at realtime!. Therefore each one is reflected to the other as a mirror.docker exec -it <containername> command it opens the expected shell within the same container but isolated about the content/interaction against of the two others tty related with the docker attach command.Now is very clear the difference between attach and exec.
Therefore the origin of the confusion is when in the host exists only two tty, one is for docker attach and the other for docker exec, therefore for this specific scenario it seems both are equals.
2015: There was a commit PR which added to the doc:
The answer to "Docker. How to get bash\ssh inside ran container (
run -d)?" illustrates the difference:As mentioned in this issue:
The same issue adds:
Update 2022: See more with "Containers 101: attach vs. exec - what's the difference?" (Dec. 2021) from Ivan Velichko:
Extract:
Bart reminds us in the comments that
docker execruns a new command in a running container. Not a "totally new one".