The Docker exec command is a very useful command for interacting with your running docker containers. When working with Docker you will likely have the need to access the shell or CLI of the docker containers you have deployed, which you can do using docker exec
.
In this article, we’ll take a detailed look at what you can do with it by using some practical examples, including:
Docker Exec Command Overview
The exec command lets you run a command in a running container or open a shell session to a running container. You can get help for running the command by running:
$ docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container </group|gid></name|uid>
The docker exec syntax is as follows:
$ docker exec <options> <container> <command>
A typical example of running the command might look like:
$ docker exec cranky_spence uptime
All I have done here is asked docker to run the ‘uptime’ command in my running container (named cranky_spence). This is a simple example, which will return the following:
$ docker exec -u 1 cranky_spence uptime 19:09:14 up 1 day, 16:37, 0 users, load average: 0.01, 0.02, 0.00
By default, the command will run in the default directory of the container, though this can be changed using the –workdir parameter. . The command to be run should be an executable, but this can be followed by parameters/arguments. Now, lets explore some of the options available to us.
Docker Exec -it
The interactive and tty options when running the docker exec command are very useful. When running “docker exec” with the “-it” option you can run a command interactively – one create use case for this is to run a bash shell and connect to it interactively. I’ll go into this below!
How to open a bash shell into a running container
This is by far one of the most common questions around this command. How do I run a bash shell to enter into a container directly? The answer is to use docker exec -it
. For example:
$ docker exec -it cranky_spence /bin/bash root@4fb1a51f3cbd:/# uptime 19:35:12 up 1 day, 16:46, 0 users, load average: 0.01, 0.04, 0.01 root@4fb1a51f3cbd:/#
In the example above, I have ran the /bin/bash command to get an interactive bash shell in my container, then I’ve ran the uptime command interactively. Once done with the interactive shell you can end the session by typing ‘exit’. Note that this doesn’t affect the running state of the container.
Depending on the container, you may be able to get the interactive shell just by running ‘bash’:
$ docker exec -it cranky_spence bash
Docker Exec as Root
A common question is how do you run a docker exec command as root. The answer is to use the -u
or --user
option when running the command:
-u, --user string Username or UID (format: <name|uid>[:<group|gid>]) </group|gid></name|uid>
To run a command as root, we need to specify user ‘0’ when running the command. We can test this by running the whoami command (note my container name is cranky_spence):
$ docker exec -u 0 cranky_spence whoami root
To verify this works, we can run the same command but with a different user:
$ docker exec -u 1 cranky_spence whoami daemon
View a Containers Running Processes
You can view the running processes inside a Linux container by running the ‘ps’ command, just as you would normally. For example:
$ docker exec -it centos ps -aux
Running Multiple Commands
So how do you run a command to run multiple commands in the container? It’s possible to execute two commands with docker exec or even more! You can do so by running bash, and send the multiple commands through as a string. For example:
$ docker exec cranky_spence /bin/bash -c "uptime; whoami; date" 19:49:36 up 1 day, 17:01, 0 users, load average: 0.00, 0.02, 0.00 root Mon Feb 10 19:49:36 UTC 2020
In the example above, I’ve ran /bin/bash using a non-interactive docker exec command, and successfully ran the uptime, whoami and date commands, the output from each is all returned.
Running a Command from a Specific Location
It’s possible to to run a command in a specific directory on a container. For example, you may have a Python script on the container, that you wish to execute. To do so you can use the -w
switch to set the working directory for running the command. For example, you could:
$ docker exec -w /usr/scripts nginx myscript.py
In the above example, it will run a Python script called ‘myscript.py’ which is in ‘/usr/scripts’ on a container called ‘nginx’.
What’s the Difference between Docker Run and Docker Exec?
Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container! Easy!
Docker Attach vs Exec
Docker also has a command called docker attach
, which attaches to the process that runs in the container with PID 1. Something to be aware of here is that once you exit out of this, your container will be in the stopped state. Find out more about docker attach here.
Final Word
Don’t forget to check out the official documentation here and check out my page on docker certification here. Hopefully this article has improved your knowledge of the docker exec command!