Techiio-author
Started by Peter DanielsSep 24, 2021

Open
Difference between attached and interactive mode in Docker

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

My current understanding about what happens when we attach a container to a terminal is that the STD IN/OUT/ERR of the current terminal is connected to the STD IN/OUT/ERR of the startup command process (PID 1) of the container. Running the container interactively using -i flag also seem to do something similar. Whats the difference here?

Also from what I have tested, attaching a busybox container (with the default startup command sh itself) doesn't give the shell prompt while running it interactively is giving the shell prompt. How can we explain this?

1 Replies

Techiio-commentatorNeel Dey replied 7 months ago0 likes0 dislikes

When a container is started using /bin/bash then it becomes the container 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 the 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 the -d flag then exiting out of the container will not stop the container, whether you use to attach or exec to get inside.

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance