Connecting to a running Docker container is helpful when you want to see what is happening inside the container. If the Docker container doesn’t work as expected, you can attach to the container or get a shell to the container and run commands such as
top. You can also enter the container, install new packages, and build a new Docker image from it.
In this tutorial, we will explain how to attach to the container’s main running process and how to get a shell to a running container.
Attach to a Container
Although it is possible to run multiple processes in a container, most docker containers are running only a single process. The command that is executed when starting a container is specified using the
docker attach command allows you to attach your terminal to the running container. This is useful when you want to see what is written in the standard output in real-time, or to control the process interactively.
To better understand how the
attach command works let’s run a new detached Nginx container using the official Nginx image.
docker container run --name my_nginx -d -p 8080:80 nginx
-p 8080:80 option tells Docker to bind port 8080 of the container to port 80 on the host machine.
List the containers to make sure the “my_nginx” container is running:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e1c4974a8d8 nginx "nginx -g 'daemon of…" 3 minutes ago Up 2 seconds 0.0.0.0:8080->80/tcp my_nginx
Attach to the container using the container’s ID or name:
docker container attach my_nginx
The default command of the nginx image which is executed when you run the container is set to
CMD ["nginx", "-g", "daemon off;"]. When you run the
attach command your terminal attaches to the
127.0.0.1:8080 in your browser and you can watch the output of the nginx process in real time.
192.168.33.1 - - [04/Oct/2019:21:12:28 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36" "-" 192.168.33.1 - - [04/Oct/2019:21:12:28 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.33.71:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36" "-"
To get access to the container logs you should prefer using the
docker logs command.
To detach from the container without stopping it, use the
CTRL-q key combination. Pressing
CTRL-c stops the container.
If the running processes you are attaching to accepts input, you can send instructions to it.
Get a Shell to a Container
docker exec command allows you to run commands inside a running container.
To see how the
exec command works and how it can be used to enter the container shell, first, start a new container. We’ll use the official MySQL image:
docker container run --name my_mysql -d mysql
This will create a container named “my_mysql”.
To execute a command inside the container run the following command:
docker container exec -it my_mysql ls /var
-i option stands for interactive, and
-t tells Docker to allocate a pseudo TTY device. The
ls command will list all files and directories inside container’s
backups cache lib local lock log mail opt run spool tmp
To get a shell to the container i.e., to enter inside the container, start a new shell session by executing the shell binary. You can use
bash, or any other shell that is included in the image.
The command below will create a new Bash session inside the container:
docker container exec -it my_mysql /bin/bash
Your command prompt will change, indicating that you’re now working on the container shell.
From here, you can run commands in the same way as you would do on any other Linux server. For example, to get a list of the current environment variables type
The output will look something like this:
HOSTNAME=e0214d97e0fe MYSQL_ROOT_PASSWORD=my-secret-pw PWD=/ HOME=/root MYSQL_MAJOR=8.0 GOSU_VERSION=1.7 MYSQL_VERSION=8.0.17-1debian9 TERM=xterm SHLVL=1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin _=/usr/bin/env
docker exec and
docker attach commands allow you to connect to a running container. To get an interactive shell to a container, use the
exec command to start a new shell session. The
attach command attaches your terminal to a running container.
If you have any questions, please leave a comment below.