0
0
Dockerdevops~5 mins

Executing commands with docker exec - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to run a command inside a running container to check its status or fix something. The docker exec command lets you do that by opening a terminal inside the container or running a quick command.
When you want to check the logs or files inside a running container without stopping it
When you need to run a quick fix or update inside a container, like installing a package
When you want to open an interactive shell inside a container to explore or debug
When you want to run a command inside a container to check its environment or settings
When you want to test a command inside a container before adding it to the Dockerfile
Commands
This command starts a new nginx container in the background with the name example-nginx and maps port 8080 on your computer to port 80 inside the container.
Terminal
docker run -d --name example-nginx -p 8080:80 nginx:1.23.3
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
-d - Run container in detached mode (in the background)
--name - Assign a name to the container for easy reference
-p - Map host port to container port
This runs the ls command inside the running example-nginx container to list the files in the default web folder.
Terminal
docker exec example-nginx ls /usr/share/nginx/html
Expected OutputExpected
50x.html index.html
This opens an interactive shell inside the example-nginx container so you can run commands manually.
Terminal
docker exec -it example-nginx sh
Expected OutputExpected
#
-i - Keep STDIN open even if not attached
-t - Allocate a pseudo-TTY for interactive shell
This command exits the interactive shell inside the container and returns you to your normal terminal.
Terminal
exit
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: docker exec lets you run commands inside a running container without stopping it.

Common Mistakes
Trying to use docker exec on a container that is not running
docker exec only works on running containers and will give an error if the container is stopped
Make sure the container is running by using docker ps before running docker exec
Forgetting to use -it flags when opening an interactive shell
Without -it, you won't get a usable interactive terminal and commands may not work as expected
Always use docker exec -it when you want to open a shell inside the container
Using docker exec to run commands that require root but not specifying the user
The default user may not have permission to run some commands inside the container
Use the -u flag with docker exec to specify the root user if needed, e.g., docker exec -u 0 -it container_name sh
Summary
Start a container with docker run and give it a name for easy access.
Use docker exec to run commands inside the running container without stopping it.
Use docker exec -it to open an interactive shell inside the container for manual commands.