![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
The docker exec command allows users to run commands within an already deployed container.
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.
You’ll only need a running instance of the Docker runtime engine installed on a supported platform. I’ll demonstrate this 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.
Before you can install Docker on Ubuntu Server, you must first add the official Docker GPG key with the command:
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:
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
Update apt with:
sudo apt-get update
Install Docker with the command:
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:
sudo usermod -aG docker $USER
Log out and log back in so the changes take effect.
Congrats, Docker is now ready to go.
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:
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:
docker ps
You’ll only need the first four characters of the ID.
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:
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:
apt-get update apt-get upgrade -y
After the upgrade completes, you can exit the shell with the command:
exit
Let’s simplify the process.
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:
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:
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:
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:
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:
docker exec ID cat /usr/share/nginx/html/index.html
The output should simply be:
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.
Here are some common errors that may occur when using the docker exec command:
A: The exec command allows you to run commands within an already deployed container, so you can interact with your running containers.
A: No, by default, the docker exec command requires a running container. If you want to execute a command outside of the existing container’s shell, you need to create an interactive session using docker exec -it ID /bin/bash.
A: The first four characters of the container ID (e.g., “1234”) can be used as part of the command.
A: Yes, you can run commands like apt-get update && apt-get upgrade inside a running container using docker exec.
A: The output of the executed command is displayed in your terminal. If you want to capture the output for further processing or logging, use redirection operators (e.g., >, >>) as needed.
A: Yes, by default, Docker executes each command sequentially. To execute a series of commands one after another without waiting for input from the shell, use the && operator between them (e.g., apt-get update && apt-get upgrade -y).
A: If you run a command that fails within an existing container, Docker exits and terminates your interactive session. To avoid this, use the -n option with docker exec, which prevents Docker from waiting for input (e.g., docker exec -it ID /bin/bash -n apt-get update && …).
A: No, by default, Docker does not support piping or redirecting output/input within a container. To achieve this functionality, use tools like docker run –rm -it <image> /bin/bash, which creates an isolated environment with no persistent file system.
A: You can exit the interactive session using the exit command or by closing your terminal.