Terminus
Execute in a Docker Container

Execute in a Docker Container

The short answer

In Docker, to execute a command inside a container running in detached mode (i.e., the background), you can use the [.inline-code]docker exec[.inline-code] command as follows:

$ docker exec <container> <command>

Where:

  • [.inline-code]container[.inline-code] is the name or the identifier of a container.
  • [.inline-code]command[.inline-code] is the command you want to run inside of the container.

For example, the following command will execute the [.inline-code]ls[.inline-code] command inside of the container identified by the name [.inline-code]http-server[.inline-code] and write the output of the command to the standard output of the terminal:

$ docker exec http-server ls

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering the [.inline-code]execute command in docker container[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]docker[.inline-code] command that can be quickly inserted into your shell by pressing [.inline-code]CMD+ENTER[.inline-code].

[#execute-commands-as-non-root] Executing commands as a non-root user [#execute-commands-as-non-root]

By default, the specified command will be executed inside of the container as the [.inline-code]root[.inline-code] user.

To execute a command as a non-root user, you can use the [.inline-code]docker exec[.inline-code] command with the [.inline-code]-u[.inline-code] flag (short for [.inline-code]--user[.inline-code]) as follows:

$ docker exec -u >username< -it >container< >command<

Where:

  • [.inline-code]username[.inline-code] is the username or UID of the user the command will be executed as in the form of [.inline-code]<name|uid>[:<group|gid>][.inline-code].

For example, the following command will execute the [.inline-code]ls[.inline-code] command inside of the container named [.inline-code]ubuntu[.inline-code] as the [.inline-code]johndoe[.inline-code] user:

 $ docker exec -u johndoe ubuntu ls

[#execute-commands-in-a-directory] Executing commands in a specific directory [#execute-commands-in-a-directory]

By default, the specified command will be executed inside of the container in the home directory of the user specified through the [.inline-code]-u[.inline-code] flag, or by default, in the root directory (i.e., [.inline-code]/[.inline-code]).

To change the working directory the command will be executed in, you can use the [.inline-code]-w[.inline-code] flag (short for [.inline-code]--workdir[.inline-code]) as follows:

 $ docker exec -w <path> <container> <command>

Where:

  • [.inline-code]path[.inline-code] is the absolute path of the working directory.

For example, the following command will output the content of the [.inline-code]20240321.log[.inline-code] file located in the [.inline-code]/app/data/logs/[.inline-code] directory using the [.inline-code]cat[.inline-code] command in the container identified by the ID [.inline-code]729fae70158c[.inline-code]:

 $ docker exec -w /app/data/logs 729fae70158c cat 20240321.log

[#start-an-interactive-shell-session] Starting an interactive shell session [#start-an-interactive-shell-session]

To facilitate the execution of commands in a container for debugging, performing administrative tasks, or simply testing, you can start an interactive shell session that provides direct access to the container's shell using the [.inline-code]-it[.inline-code] flags (short for [.inline-code]--interactive[.inline-code] and [.inline-code]--tty[.inline-code]) as follows:

 $ docker exec -it <container> <shell>

Where:

  • [.inline-code]-it[.inline-code] is used to interactively execute commands inside a container.
  • [.inline-code]shell[.inline-code] is the absolute path to a shell binary installed in the container.

For example, the following command will start an interactive shell session using Bash in the container named [.inline-code]ubuntu[.inline-code]:

$ docker exec -it ubuntu /bin/bash

You can learn more about containers and Bash by reading our other article on how to run a Bash shell in Docker.

[#use-shell-expansions] Using shell expansions [#use-shell-expansions]

By default, any shell expansions specified in the command executed by the [.inline-code]docker exec[.inline-code] command will be expanded by the shell the command is executed from on the local machine.

For example, when running the following command, the [.inline-code]~[.inline-code] expression will be expanded into the path of the user's home directory on the local machine:

$ docker exec 729fae70158c ls ~

To get around this problem, you can pass your command as sub-command of the [.inline-code]/bin/bash -c[.inline-code] command as follows:

$ docker exec <container> /bin/bash -c '<command>'

For example, when running the following command, the [.inline-code]~[.inline-code] expression will be expanded into the path of the [.inline-code]johndoe[.inline-code] user's home directory (i.e., [.inline-code]/home/johndoe[.inline-code]) in the container:

$ docker exec -u johndoe 729fae70158c /bin/bash -c 'ls ~'

[#set-environment-variables] Executing a command with temporary environment variables [#set-environment-variables]

By default, the commands executed through the [.inline-code]docker exec[.inline-code] command inherit the environment variables set at the time the container is created.

To override existing environment variables or set additional ones, you can use the [.inline-code]-e[.inline-code] flag (short for [.inline-code]--env[.inline-code]) as follows:

$ docker exec -e <variable>=<value> [-e <variable>=<value> …] <container> <command>

Where:

  • [.inline-code]variable[.inline-code] is the name of the environment variable.
  • [.inline-code]value[.inline-code] is the value assigned to that variable.

For example, the following command will set the [.inline-code]NODE_ENV[.inline-code] environment variable as [.inline-code]development[.inline-code] and execute the [.inline-code]node index.js[.inline-code] command in the [.inline-code]/app[.inline-code] directory of the container named [.inline-code]web-server[.inline-code]:

$ docker exec -w /app -e NODE_ENV=development web-server node index.js

[#use-environment-files] Using an environment file [#use-environment-files]

Alternatively, you can use the [.inline-code]--env-file[.inline-code] flag to pass a file containing a list of environment variables as follows:

$ docker exec --env-file <path> <container> <command>

Where:

  • [.inline-code]path[.inline-code] is the relative or absolute path to the environment file.

For example, the following command will use the variables specified in the [.inline-code].env.dev[.inline-code] file located in the current working directory and execute the [.inline-code]npm run test[.inline-code] command in the [.inline-code]/app[.inline-code] directory of the container named [.inline-code]web-server[.inline-code]:

$ docker exec -w /app --env-file .env.dev web-server npm run test

If you want to save the changes made to a container using the [.inline-code]docker exec[.inline-code] command, you can read our other article on how to save a Docker container as an image.