DevOpsDockerDockersHow to

Docker container attach

Suppose you have a running container. It is currently detached from your terminal session.
You can use the container attach command to bring that container’s executing process to be the foreground process of your terminal session. Let’s use the web-server container as an example:

# run a container detached
docker container run --detach -it --name web-server1 -p 80:80 nginx
# show that the container is running
docker container ps
# attach to the container
# Usage: docker container attach [OPTIONS] CONTAINER
docker container attach web-server1
# issue a Ctrl + PQ keystroke to detach (except for Docker on Mac, see
below for special Mac instructions)
# again, show that the container is running detached.
docker container ps

When you attach to the running container, its executing command becomes the foreground process for your terminal session.
To detach from the container, you need to issue a Ctrl + PQ keystroke.
If you issue a Ctrl + C keystroke, the container’s executing process will receive a sig-term signal and will terminate, which in turn will exit the container.
This is usually not desired. So remember to detach by using a Ctrl + PQ keystroke. However, there is a known issue on macOS: for Docker on Mac, the Ctrl + PQ keystroke does not work, and unless you use another parameter, the –sig-proxy=false parameter, on the attach command, you will not be able to detach from the container without terminating it with a Ctrl + C keystroke:

# when you are using Docker for Mac, remember to always add the "--sigproxy=
false" parameter
docker attach --sig-proxy=false web-server1

When you provide the –sig-proxy=false parameter to the attach command, you can issue a Ctrl + C keystroke to the attached container and it will detach without sending the sig-term signal to the container process, thus keeping the container running, once again detached from your terminal session:

docker container run --detach --name server1 -p 80:80 nginx

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *


Please type the text above:

Check Also