How to use docker exec to run commands in a Docker Container

0 Shares
0
0
0
0

Introduction

Docker is a containerization tool that helps developers create and manage portable, Linux-compatible containers.

When developing or deploying containers, you often need to look inside a running container to check its current state or troubleshoot a problem. For this purpose, Docker provides the docker exec command to execute applications in running containers.

In this tutorial, we will learn about the docker exec command and how to use it to run commands and get an interactive shell in a Docker container.

Prerequisites

This tutorial assumes that you have already installed Docker and your user has permission to run docker. If you need to run docker as root user, please remember to add sudo to the commands in this tutorial.

For more information about using Docker without sudo access, please see the Running Docker Commands Without Sudo section in our How to Install Docker tutorial.

Launching a test container

To use the docker exec command, you need a running Docker container. If you don't already have one, start a test container with the following docker exec command:

docker run -d --name container-name alpine watch "date >> /var/log/date.log"

This command creates a new Docker container from the official Alpine image. This is a popular Linux container image that uses Alpine Linux, a lightweight and minimal Linux distribution.

We use the -d flag to detach the container from the terminal and run it in the background. --name container-name names the container. You can choose any name you like here or leave this out entirely to have Docker automatically generate a unique name for the new container.

Next we have alpine which specifies the image we want to use for the container.

And finally, we have “date >> /var/log/date.log”. This is the command we want to run in the container. By default, the clock will run the command you give it every two seconds. The command that the clock will run in this case is date >> /var/log/date.log. date prints the current date and time, like this:

Output
Fri Jul 23 14:57:05 UTC 2021

The >> /var/log/date.log section redirects the output from the date command and appends it to the file /var/log/date.log. Every two seconds a new line is appended to the file, and after a few seconds it will look something like this:

Output
Fri Jul 23 15:00:26 UTC 2021
Fri Jul 23 15:00:28 UTC 2021
Fri Jul 23 15:00:30 UTC 2021
Fri Jul 23 15:00:32 UTC 2021
Fri Jul 23 15:00:34 UTC 2021

Next, we'll learn how to find the names of Docker containers. This is useful if you already have a container you're targeting but aren't sure what its name is.

Finding the Docker container name

We need to provide docker exec with the name (or container ID) of the container we want to work with. We can find this information using the docker ps command:

docker ps

This command lists all Docker containers running on the server and provides some high-level information about them:

Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76aded7112d4 alpine "watch 'date >> /var…" 11 seconds ago Up 10 seconds container-name

In this example, the container ID and name are highlighted. You can use either to tell docker exec which container to use.

If you want to change the name of your container, use the docker rename command:

docker rename container-name new-name

Next, we will run several examples of using docker exec to execute commands in a Docker container.

Running an interactive shell in a Docker container

If you need to launch an interactive shell inside a Docker container, perhaps to explore the file system or debug running processes, use docker exec with the -i and -t flags.

The -i flag keeps the input to the container open, and the -t flag creates a pseudo-terminal that the shell can connect to. These flags can be combined as follows:

docker exec -it container-name sh

This will run the shell in the specified container and give you an initial shell prompt. To exit the container, type exit and ENTER Press:

exit

If your container image includes a more advanced shell like bash, you can replace sh with bash above.

Running a non-interactive command in a Docker container

If you need to run a command inside a running container, but don't need to interact, use the docker exec command without any flags:

docker exec container-name tail /var/log/date.log

This command will run tail /var/log/date.log on the container container name and print the results. By default, the tail command prints the last ten lines of a file. If you run the test container we set up in the first section, you will see something like this:

Output
Mon Jul 26 14:39:33 UTC 2021
Mon Jul 26 14:39:35 UTC 2021
Mon Jul 26 14:39:37 UTC 2021
Mon Jul 26 14:39:39 UTC 2021
Mon Jul 26 14:39:41 UTC 2021
Mon Jul 26 14:39:43 UTC 2021
Mon Jul 26 14:39:45 UTC 2021
Mon Jul 26 14:39:47 UTC 2021
Mon Jul 26 14:39:49 UTC 2021
Mon Jul 26 14:39:51 UTC 2021

This is essentially the same as opening an interactive shell for the Docker container (as was done in the previous step with docker exec -it container-name sh ) and then running the tail /var/log/date.log command. However, instead of opening a shell, running the command, and then closing the shell, this command returns the same output in a single command without opening a pseudo-terminal.

Running commands in an alternate directory in a Docker container

To run a command in a specific directory of your container, use the –workdir flag to specify the directory:

docker exec --workdir /tmp container-name pwd

This example command sets the /tmp directory as the working directory, then runs the pwd command, which prints the current working directory:

Output
/tmp

The pwd command has confirmed that the working directory is /tmp.

Running commands as a different user in a Docker container

To run a command as a different user inside your container, add the --user flag:

docker exec --user guest container-name whoami

This uses the guest user to run the whoami command in the container. The whoami command prints the current username of the user:

Output
guest

The whoami command confirms that the current user of the container is the guest.

Passing environment variables to a Docker container

Sometimes it is necessary to pass environment variables to a container along with the run command. The -e flag allows you to specify an environment variable:

docker exec -e TEST=sammy container-name env

This command sets the TEST environment variable to sammy, then runs the env command inside the container. The env command then prints all the environment variables:

Output
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=76aded7112d4
TEST=sammy
HOME=/root

The TEST variable is set to sammy.

To set multiple variables, repeat the -e flag for each:

docker exec -e TEST=sammy -e ENVIRONMENT=prod container-name env

If you want to pass a file full of environment variables, you can do so with the –env-file flag.

First, create the file with a text editor. We'll open a new file with nano here, but you can use any editor you're comfortable with:

nano .env

We use .env as the file name because it is a popular standard for using these types of files to manage information outside of version control.

Write your KEY=value variables in the file, one per line, as shown below:

TEST=sammy
ENVIRONMENT=prod

Save and close the file. To save the file and exit nano, press CTRL+O, then ENTER to save, then CTRL+X to exit.

Now run the docker exec command and specify the correct file name after –env-file:

docker exec --env-file .env container-name env
Output
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=76aded7112d4
TEST=sammy
ENVIRONMENT=prod
HOME=/root

Two variables are set in the file.

You can specify multiple files using multiple –env-file flags. If the variables in the files overlap, whichever file is listed in the last command will override the previous files.

Common errors

When using the docker exec command, you may encounter a few common errors:

Error: No such container: container-name

The No such container error means that the specified container does not exist and may indicate a misspelling of the container name. Use docker ps to list your running containers and double-check the name.

Error response from daemon: Container 2a94aae70ea5dc92a12e30b13d0613dd6ca5919174d73e62e29cb0f79db6e4ab is not running

This message running means that the container exists but is stopped. You can start the container with docker start container-name

Error response from daemon: Container container-name is paused, unpause the container before exec

The error Container is paused explains the problem well. Before continuing, you need to unpause the container with docker unpause container-name .

Result

In this tutorial we learned how to execute commands in a running Docker container, along with some command line options when doing so.

For more information about Docker in general, please visit our Docker tag page, which has links to Docker tutorials, Docker-related Q&A pages, and more.

Leave a Reply

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

You May Also Like