Report this

What is the reason for this report?

How to Use docker exec to Access and Manage Containers

Updated on January 27, 2026
How to Use docker exec to Access and Manage Containers

Introduction

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

When developing or deploying containers you’ll often need to look inside a running container to inspect its current state or debug a problem. To this end, Docker provides the docker exec command to run programs in already 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.

Deploy your Docker applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Key Takeaways:

  • The docker exec command enables you to run commands or access a shell inside an already running Docker container without restarting it.
  • Using docker exec -it allows you to start an interactive shell inside a container for debugging, inspection, or filesystem exploration.
  • Non-interactive commands can be executed with docker exec to run a single command and immediately return its output without opening a shell session.
  • Options such as --workdir, --user, -e, and --env-file provide control over the execution context when running commands inside a container.
  • The docker exec command only works with containers that are currently running and will fail if the container is stopped or paused.
  • Running resource-intensive or unnecessary commands with docker exec can affect container performance, so such operations should be used cautiously.

Prerequisites

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

For more information on using Docker without sudo access, please see the Executing the Docker Command Without Sudo section of our How To Install Docker tutorial.

Starting a Test Container

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

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

Note: The official Alpine image includes the watch utility. If you are using a more minimal image, you may need to install it or use an alternative loop-based command.

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, minimal Linux distribution.

We use the -d flag to detach the container from our terminal and run it in the background. --name container-name will name the container container-name. You could choose any name you like here, or leave this off 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 watch "date >> /var/log/date.log". This is the command we want to run in the container. watch will repeatedly run the command you give it, every two seconds by default. The command that watch will run in this case is date >> /var/log/date.log. date prints the current date and time, like this:

Output
Tue Jan 27 07:12:04 UTC 2026

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

Output
Tue Jan 27 07:21:04 UTC 2026 Tue Jan 27 07:21:06 UTC 2026 Tue Jan 27 07:21:08 UTC 2026 Tue Jan 27 07:21:10 UTC 2026 Tue Jan 27 07:21:12 UTC 2026

In the next step, we’ll learn how to find the names of Docker containers. This will be useful if you already have a container you’re targeting but are not sure what its name is.

Finding the Name of a Docker Container

We’ll 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 of the 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 may use either to tell docker exec which container to use.

If you’d like to rename your container, use the docker rename command:

docker rename container-name new-name

Next, we’ll 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 start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags.

The -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal to which the shell can attach. These flags can be combined like this:

docker exec -it container-name sh

This will run the sh shell in the specified container, giving you a basic shell prompt. To exit back out of the container, type exit then press ENTER:

  1. exit

If your container image includes a more advanced shell such as bash, you could replace sh with bash above.

Running a Non-interactive Command in a Docker Container

If you need to run a command inside a running Docker container, but don’t need any interactivity, 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-name container, and output the results. By default, the tail command will print out the last ten lines of a file. If you’re running the demo container we set up in the first section, you will see something like this:

Output
Tue Jan 27 07:23:38 UTC 2026 Tue Jan 27 07:23:40 UTC 2026 Tue Jan 27 07:23:42 UTC 2026 Tue Jan 27 07:23:44 UTC 2026 Tue Jan 27 07:23:46 UTC 2026 Tue Jan 27 07:23:48 UTC 2026 Tue Jan 27 07:23:50 UTC 2026 Tue Jan 27 07:23:52 UTC 2026 Tue Jan 27 07:23:54 UTC 2026 Tue Jan 27 07:23:56 UTC 2026

This achieves a similar result to running the command inside an interactive shell, but without opening a shell session or allocating a pseudo-terminal.

Running Commands in an Alternate Directory in a Docker Container

To run a command in a certain 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 out the present 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 will use the guest user to run the whoami command in the container. The whoami command prints out the current user’s username:

Output
guest

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

Passing Environment Variables into a Docker Container

Sometimes you need to pass environment variables into a container along with the command to run. The -e flag lets you specify an environment variable:

docker exec -e TEST=sammy container-name env

This command sets the TEST environment variable to equal sammy, then runs the env command inside the container. The env command then prints out 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 one:

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

If you’d like to pass in a file full of environment variables you can do that with the --env-file flag.

First, make 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’re using .env as the filename, as that’s a popular standard for using these sorts of files to manage information outside of version control.

Write your KEY=value variables into the file, one per line, like the following:

.env
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, specifying the correct filename 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

The two variables in the file are set.

You may specify multiple files by using multiple --env-file flags. If the variables in the files overlap each other, whichever file was listed last in the command will override the previous files.

Common Errors and Debugging

When using the docker exec command, you may encounter a few common errors and issues. Here are some common errors and their solutions:

Error: “Container not found”

Error: No such container: container-name

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

Error: “Permission denied”

Error response from daemon: Permission denied

The Permission denied error typically occurs when the user running the docker exec command does not have sufficient permissions to access the container. Ensure that the user has the necessary permissions or run the command with sudo.

Error: “Container is not running”

Error response from daemon: Container 2a94aae70ea5dc92a12e30b13d0613dd6ca5919174d73e62e29cb0f79db6e4ab is not running

This error message indicates that the specified container exists, but it is not currently running. This can happen if the container was previously started and then stopped, or if it crashed or exited due to an error. To resolve this issue, you can start the container again using the docker start command, followed by the name of the container:

docker start container-name

Once you’ve started the container, you should be able to execute commands within it using docker exec.

Error: “Container is paused”

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

The Container is paused error indicates that the specified container is currently in a paused state. This means that the container is not running, but it is not stopped either. Before you can execute commands within the container, you need to unpause it using the docker unpause container-name command.

Fixes for common issues when using docker exec

To avoid common issues when using docker exec, ensure that:

  • The container name is correct and the container is running.
  • The user running the command has sufficient permissions.
  • The container is not paused.
  • The command is correctly formatted and the options are used correctly.

Best Practices and Workflow Tips

When running heavy commands using docker exec, it’s essential to ensure that the command execution does not significantly impact the performance of your container. Here are some tips to help you optimize the performance of your container when using docker exec:

  • Use the --detach flag for long-running commands: When running a command that takes a long time to complete, use the --detach flag to detach the command from the terminal. This allows the command to run in the background, freeing up your terminal for other tasks. For example:

    docker exec --detach container-name command
    
  • Optimize your command: Optimize the command you’re running to reduce its execution time. This might involve splitting a large task into smaller, more manageable parts or using more efficient algorithms. For example:

    docker exec container-name optimized-command
    
  • Use a separate container for heavy tasks: If you have a task that requires significant resources or processing power, consider running it in a separate container. This isolates the task from your main container, ensuring that it does not impact the performance of your main application. For example:

    docker run --name heavy-task-container heavy-task-image
    
  • Monitor container performance: Regularly monitor the performance of your container to identify potential bottlenecks or areas for optimization. This can be done using tools like docker stats or third-party monitoring solutions. For example:

    docker stats container-name
    
  • Avoid running unnecessary commands: Only run commands that are necessary for your application’s operation. Avoid running unnecessary or redundant commands that can slow down your container. For example:

    docker exec container-name necessary-command
    

By following these tips, you can ensure that running heavy commands using docker exec does not significantly impact the performance of your container.

FAQs

1. What is the docker exec command?

The docker exec command is a Docker command that allows you to run a command inside a running Docker container. It provides a way to execute a command in a container that is already running, without having to start a new container. This is particularly useful for debugging, inspecting, or modifying the state of a running container.

For example, to run a simple ls command inside a running container, you would use the following command:

docker exec <container-name> ls

This command will execute the ls command inside the container and display the output in your terminal.

2. How do I access the shell of a running container using docker exec?

To access the shell of a running container using docker exec, you can use the -it flags. The -i flag allows you to interact with the container, and the -t flag allocates a pseudo-TTY to the container. This allows you to interact with the container as if you were sitting in front of it.

Here’s an example of how to access the shell of a running container:

docker exec -it <container-name> bash

This command will open a new shell session inside the container, allowing you to interact with it as if you were logged in directly.

3. Can I run commands as a specific user inside a Docker container?

Yes, you can run commands as a specific user inside a Docker container using the --user flag. This flag allows you to specify the user ID or name to use when running the command.

For example, to run a command as the root user inside a container, you would use the following command:

docker exec --user root <container-name> command

This command will execute the specified command inside the container as the root user.

4. How do I run multiple commands inside a container using docker exec?

To run multiple commands inside a container using docker exec, you must invoke a shell such as sh or bash and pass the commands using the shell’s -c flag. The commands should be separated by semicolons.

Here’s an example of how to run multiple commands inside a container:

docker exec <container-name> sh -c "command1; command2; command3"

This command will execute command1, command2, and command3 sequentially inside the container.

5. What should I do if docker exec does not work?

If docker exec does not work as expected, there are a few things you can try to troubleshoot the issue:

  1. Check the container status: Ensure that the container is running by using docker ps. If the container is not running, start it using docker start.
  2. Verify the container name: Make sure you are using the correct container name or ID. You can list all running containers using docker ps to verify the name.
  3. Check the command syntax: Ensure that the command you are trying to execute is correct and properly formatted.
  4. Check the container’s configuration: If you are trying to execute a command that requires specific permissions or access, ensure that the container is configured to allow it.
  5. Check the Docker version: Ensure that you are running a compatible version of Docker. You can check the Docker version using docker --version.

If none of these steps resolve the issue, you may want to consult the Docker documentation or seek further assistance from a Docker expert.

Conclusion

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

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

For help with installing Docker, take a look at How To Install and Use Docker on Ubuntu.

Next, you might want to explore more advanced Docker topics. Here are some tutorials to follow:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: Getting Started With Cloud Computing

This curriculum introduces open-source cloud computing to a general audience along with the skills necessary to deploy applications and websites securely to the cloud.

About the author(s)

Brian Boucheron
Brian Boucheron
Author
See author profile

Senior Technical Writer at DigitalOcean

Anish Singh Walia
Anish Singh Walia
Author
Sr Technical Writer
See author profile

I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix

Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Still looking for an answer?

Was this helpful?


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Great tutorial! I was wondering—when using docker exec, are there any best practices or security concerns to keep in mind, especially when running commands in production containers?

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.