This post will cover topic related to ‘Executing Commands Inside Docker Containers’ with multiple docker command examples and different scenerios. So this will help you to understand the command docker and options available in it. Also this post will explain you how to use docker command.
When working with Docker, the docker exec
command allows you to execute commands inside a running Docker container. This is particularly useful for debugging, inspecting the container’s state, or performing administrative tasks without needing to start a new container instance.
Here are several examples demonstrating the usage of docker exec
:
Example 1: Listing files inside a container named webserver
:
docker exec webserver ls -l /var/www/html
This command executes ls -l /var/www/html
inside the webserver
container, displaying a detailed list of files in the specified directory.
Example 2: Running a shell inside a container named db
:
docker exec -it db /bin/bash
Using the -it
flags allows interactive terminal access (/bin/bash
), providing a shell prompt within the db
container for interactive troubleshooting or exploration.
Example 3: Installing a package inside a running container:
docker exec webserver apt-get install -y curl
This command installs the curl
package using apt-get
inside the webserver
container without interrupting its operation.
Example 4: Checking network configuration inside a container:
docker exec db ifconfig
Here, ifconfig
is executed within the db
container to display its network configuration details.
Example 5: Viewing running processes inside a container:
docker exec webserver ps aux
This command retrieves a list of currently running processes within the webserver
container.
Example 6: Modifying a file inside a container:
docker exec -i webserver sh -c 'echo "Hello, Docker!" > /var/www/html/index.html'
Using -i
allows input from stdin, and this command writes “Hello, Docker!” to /var/www/html/index.html
in the webserver
container.
Example 7: Copying files between the host and a container:
docker exec webserver mkdir /var/www/html/test
Creates a new directory named test
under /var/www/html
within the webserver
container.
Example 8: Running a command with environment variables:
docker exec -e VAR=value webserver echo $VAR
This demonstrates passing environment variables (VAR=value
) to a command (echo $VAR
) executed within the webserver
container.
Example 9: Restarting a service inside a container:
docker exec webserver systemctl restart nginx
Restarts the nginx
service within the webserver
container.
Example 10: Checking container uptime:
docker exec webserver uptime
Displays how long the webserver
container has been running since it was started.
To verify whether these commands executed correctly, you can follow these steps:
- Run the
docker ps
command to ensure the target container (webserver
,db
, etc.) is running. - Execute the respective
docker exec
command from the examples above. - Observe the output in your terminal or command prompt. Ensure that the output matches the expected result based on the command executed.
Also check similar articles.
How to Create and Run Docker Containers from an Image
How to Manage Kubernetes Plugins
How to Manage Kubernetes Networks
How to Manage Kubernetes Image Manifests
How to Manage Kubernetes Images
Discussion about this post