Apr 28, 2023

How to Execute Commands in a Running Docker Container

How to Execute Commands in a Running Docker Container

Applications running in Docker containers often require supervision and maintenance.

However, because of the self-contained and isolated nature of containers, debugging, testing, configuring, or updating these applications can prove challenging without the right tool.

In this article, you’ll learn how to use the docker exec command to execute commands in a running Docker container.

Listing running containers

The first step consists in finding the ID of the Docker container you want to execute a command into.

To do so, you can use the docker ps command to list all the currently up and running containers:

$ docker ps
CONTAINER ID    IMAGE    COMMAND                   CREATED        STATUS        PORTS                 NAMES
181786cc4f36    node     "docker-entrypoint.s…"    2 hours ago    Up 2 hours                          pensive_tereshkova
66520acca18a    nginx    "docker-entrypoint.s…"    2 hours ago    Up 2 hours    0.0.0.0:80->80/tcp    exciting_goldberg

The docker exec command

The docker exec command has the following syntax:

$ docker exec [options] container command [arg ...]

Executing a single command

To execute a single command in a running Docker container, you can use the following docker exec command:

$ docker exec <container_id> <command>

Where:

  • container_id is the ID of the container you want to execute a command into.
  • command is the command you want to execute.

For example, to output the access logs of an Nginx server running in a Docker container, you can run the following command:

$ docker exec 66520acca18a cat /var/log/nginx/access.log

Where:

  • 66520acca18a is the ID of the container obtained using the docker ps command.
  • cat is the command you want to execute.
  • /var/log/nginx/access.log is an argument of the cat command.

Starting an interactive shell session

To execute multiple commands in a running Docker container, you can use the following docker exec command to start an interactive shell session:

$ docker exec -it <container_id> /bin/bash

Where:

  • The -it flags instructs Docker to allocate a pseudo-TTY connected to the container’s standard input, allowing you to types command from within the container.
  • The /bin/bash command starts a Bash shell.

Note that if the Bash utility is not installed in your container, you can specify the absolute path of any another shell instead such as /bin/sh, /bin/zsh, etc.

For example:

$ docker exec -it 181786cc4f36 /bin/bash

Also note that when using this command, you will be automatically logged in to the container as the root user, which means that you should be extra careful when executing operations that usually require the sudo command.

Useful options

Setting up environment variables

By default, the docker exec command inherits the environment variables that are set at the time the container is created. To override global environment variables, or to set additional environment variables for the process started by docker exec you can use the -e flag as follows:

$ docker exec -e NODE_ENV=development 181786cc4f36 node /app/index.js

Changing the working directory

By default, any command supplied to the docker exec command will be executed in the working directory set at the time the container is created. To execute a command in a different directory, you can use the -w flag as follows:

$ docker exec -w /app 181786cc4f36 npm run test

Related posts