How to Use the Docker exec Command

For those who are just getting started on your journey with Docker containers, there is much to learn. Beyond pulling images and deploying basic containers, one of the first things you’ll want to understand is the exec command.
Essentially, the exec command allows you to run commands within an already deployed container. The exec command allows you to do this in two different ways…from inside or outside the container. I’m going to show you how to do both. In the end, you’ll be better prepared to interact with your running Docker containers.
What You’ll Need
The only thing you’ll need for this is a running instance of the Docker runtime engine installed on a supported platform. I’ll demonstrate on Ubuntu Server 22.04.
In case you don’t have Docker installed, let’s take care of that first. If you already have Docker installed, go ahead and jump to the next section.
Install Docker
Before you can install Docker on Ubuntu Server, you must first add the official Docker GPG key with the command:
1 |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg |
You’ll be prompted for your sudo password.
Once the GPG key is successfully added, create the necessary Docker repository, with the following command:
Install a few dependencies with this command:
1 |
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y |
Update apt with:
1 |
sudo apt-get update |
Install Docker with the command:
1 |
sudo apt-get install docker-ce docker-ce-cli containerd.io -y |
Next, you must add your user to the docker group with the command:
1 |
sudo usermod -aG docker $USER |
Log out and log back in so the changes take effect.
Congrats, Docker is now ready to go.
Deploy a Test Container
To use the exec command, we first must deploy a simple test container. For that, we’ll use the tried-and-true NGINX and deploy a container with the command:
1 |
docker run --name docker-nginx -p 8080:80 -d nginx |
After running the command, Docker should report back the ID of the container. If you miss that, you can always view it with:
1 |
docker ps |
You’ll only need the first four characters of the ID.
Access the Running Container’s Shell
Now, we can access the running container’s shell, which will allow you to run command from inside the container. This is done with the exec command like so:
1 |
docker exec -it ID /bin/bash |
Where ID is the first four characters of the running container’s ID. You should then find yourself at the running container’s bash prompt. Let’s say you want to upgrade the software. You can do so with the commands:
1 2 |
apt-get update apt-get upgrade -y |
After the upgrade completes, you can exit the shell with the command:
1 |
exit |
Let’s simplify the process.
Run a Command from Outside the Container
Thanks to the exec command, you don’t have to first access the container’s shell before running a command. Instead, you can send the command inside the container. Let’s stick with our example of updating and upgrading the running NGINX container. Again, we’ll need the container ID for this command.
To update and upgrade the software for our NGINX container (without first accessing the container), the command would be:
1 |
docker exec ID apt-get update && apt-get upgrade |
Where ID is the first four characters of the container ID.
The use of the && operator is common in Linux and makes it possible to daisy chain commands together such that they run one after another.
You can use this method to run just about any command. For example, you could view the index.html file used by NGINX with the command:
1 |
docker exec ID cat /usr/share/nginx/html/index.html |
Where ID is the first four characters of the container ID.
Let’s copy a new index.html file into the running container and then use exec to view it. Create the new file on your host with:
1 |
nano index.html |
In that file, paste the following contents:
Save and close the file. Next, copy the file to the running NGINX container with the command:
1 |
docker cp index.html ID:/usr/share/nginx/html/ |
Where ID is the ID of the running container.
Now, we can view the contents of the file with:
1 |
docker exec ID cat /usr/share/nginx/html/index.html |
The output should simply be:
1 |
Hello, New Stack |
And that’s how you use the Docker exec command. With this tool, you can better (and more efficiently) manage your running Docker containers.