Master docker exec
: Run Commands Inside Docker Containers (with Examples)
Want to peek inside your Docker containers to debug issues or run commands? This guide teaches you how to use docker exec
effectively. Learn how to access a running container, execute commands, and manage your containerized applications with ease.
What is docker exec
and How Does it Help?
Docker is a powerful tool for containerizing applications, but sometimes you need to interact directly with a running container. That's where docker exec
comes in. It allows you to execute commands within a Docker container, giving you the ability to:
- Inspect the container's state: Check file systems, running processes, and environment variables.
- Debug problems: Troubleshoot issues directly within the container environment.
- Run administrative tasks: Manage configurations, install software, or perform other maintenance tasks.
Prerequisites: Getting Ready to Use docker exec
Before diving in, make sure you have the following:
- Docker installed: Ensure Docker is properly installed and running on your system.
- Permissions to run Docker: Verify your user has the necessary permissions to execute Docker commands. If not, prepend
sudo
to your commands. - A running container: You'll need a container to experiment with. If you don't have one, create a test container as shown in the next section.
Starting a Test Container: Your Docker Playground
Let's create a simple Alpine Linux container that logs the date and time every two seconds. This will give us something to work with. Run this command in your terminal:
Here's what each part of the command does:
-d
: Runs the container in detached mode (in the background).--name my-test-container
: Assigns the name "my-test-container" to the container (you can use any name you like).alpine
: Specifies the Alpine Linux image to use.watch "date >> /var/log/date.log"
: Executes the commanddate >> /var/log/date.log
every two seconds. This appends the current date and time to the/var/log/date.log
file inside the container.
Finding the Name or ID of Your Docker Container
To use docker exec
, you need to know the name or ID of the container you want to access. Use the docker ps
command to list running containers:
The output will display information about each container, including its ID and name. You can use either the container ID or name with docker exec
.
Using docker exec
for Interactive Access: Shelling into Your Container
To get an interactive shell inside a running container, use the -it
flags with docker exec
:
-i
: Keeps input open, allowing you to type commands.-t
: Allocates a pseudo-terminal, providing a shell-like environment.
Running Single Commands: Non-Interactive Execution
Sometimes you don't need a full shell; you just want to run a single command inside the container. For example, to view the last few lines of the date log file:
This executes the tail /var/log/date.log
command within the "my-test-container" container and prints the output to your terminal.
Changing the Working Directory: Running Commands in Specific Folders
You can specify the working directory for your command using the --workdir
flag:
This sets the working directory to /tmp
inside the container and then runs the pwd
command, which prints the current working directory.
Running as a Different User: Access Control Inside Containers
To execute a command as a different user within the container, use the --user
flag:
This runs the whoami
command (which prints the current user) as the "guest" user.
Injecting Environment Variables: Dynamic Configuration
Pass environment variables into the container using the -e
flag:
This sets the TEST_VAR
environment variable to "hello world" and runs the env
command to display all environment variables inside the container.