5.1 Basics of the docker exec
Command
The docker exec
command is a tool for interacting with running containers. It lets you run commands and open interactive sessions inside a container. This is useful for administrative tasks, debugging, and monitoring. In this lecture, we'll dive deep into how to use the docker exec
command, what options are available, and provide examples of its practical use.
The docker exec
command is used to execute commands in a running container. With it, you can enter a container and interact with it as if you were directly working on the server.
Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Where:
-
CONTAINER
: the name or ID of the container you want to access. -
COMMAND
: the command you want to execute inside the container. ARG...
: arguments for the command.
Basic Usage Example
This example will run the ls /app
command inside the my_container
container, allowing you to see the contents of the /app
directory in the container.
docker exec my_container ls /app
5.2 Parameters for the docker exec Command
The docker exec command supports several options that let you configure how commands execute within a container.
1. Starting an Interactive Session
To start an interactive session, use the combination of options -i
(interactive) and -t
(pseudo-TTY)
.
docker exec -it my_container /bin/bash
In this example, an interactive Bash
terminal will be started inside the container my_container
. This lets you run commands in real time.
2. Running Commands in Detached Mode
If you need to execute a command inside a container without waiting for it to finish, you can use the -d
option (detached mode).
docker exec -d my_container touch /app/newfile.txt
In this example, the command creates an empty file named newfile.txt
in the /app
directory of the container my_container
. Control of the terminal returns immediately, without waiting for the command to complete.
3. Setting Custom Environment
You can pass environment variables into the container using the -e
parameter.
docker exec -e MY_VAR=value my_container env
This example sends the environment variable MY_VAR
into the container and runs the env
command to display all the environment variables inside the container.
5.3 Examples of Usage
Example 1: Viewing running processes inside the container
This example will run the ps aux
command inside the my_container
container and show a list of all running processes.
docker exec my_container ps aux
Example 2: Checking the status of a service in the container
This example will run the service nginx status
command inside the my_container
container and display the status of the nginx
service.
docker exec my_container service nginx status
Example 3: Installing a package inside the container
This example will update the package list and install the vim
editor inside the my_container
container.
docker exec my_container apt-get update && apt-get install -y vim
5.4 Practical Scenarios
1. Container Administration
The docker exec command lets admins perform maintenance tasks inside containers: edit configuration files, check logs, or change file permissions.
docker exec -it my_container vim /etc/nginx/nginx.conf
2. Debugging Applications
During app development and testing, the docker exec command helps developers check app status, debug, and analyze errors.
docker exec -it my_container python /app/scripts/debug.py
3. Monitoring and Diagnostics
With docker exec, you can run diagnostic commands and monitor container status: check resource usage, network connections, and other metrics.
docker exec my_container top
This command will launch the top utility inside the my_container and show current resource usage.
5.5 Additional Recommendations
1. Ensuring Security
When using the docker exec command, it's important to keep security in mind. Make sure that access to containers is restricted and only authorized users can execute commands inside the containers.
2. Task Automation
You can automate routine tasks by using scripts that utilize the docker exec command. This helps you to build complex workflows and automate container administration.
#!/bin/bash
for container in $(docker ps -q); do
docker exec $container uptime
done
This script will execute the uptime
command in all running containers and display their uptime.
3. Logs and Debugging
Sometimes, a command execution might fail. In such cases, it is useful to check the container logs to identify the cause.
docker logs my_container
GO TO FULL VERSION