![]() |
VOOZH | about |
In containerization, the `docker exec` command stands out as a powerful tool for interacting with running Docker containers. This article explores the capabilities and usage of `docker exec`, detailing how it facilitates seamless communication and control over containerized applications.
Before trying to run the Docker commands ensure that the Docker software is set up and the service is active. Check the status of the docker service with the command `systemctl status docker` If the status is active then the docker service is ready to use and perform the container operations and management. To start the docker service try to run the following command
systemctl start docker ( or ) To automatically start the docker service, Once the base OS is started try to use this command:
systemctl enable docker --now ๐ Install and start Docker serviceFor Installation refer to the Article - How to Install Docker on Linux Distributions
Table of Content
The `docker exec` is a docker command that allows users to run commands inside a running Docker container, bridging the gap between the container environment and the host system. This command opens up possibilities for troubleshooting and debugging. It performs administrative-related tasks within a live container.
The following are the options of Docker Exec Command:
| Option | Description |
|---|---|
-d, --detach | Runs the command in the background |
-i, --interactive | Keeps STDIN open even if not attached |
-t, --tty | Allocates a pseudo-TTY (useful for interactive commands) |
-u, --user | Specifies the user to run the command as |
-w, --workdir | Sets the working directory inside the container |
The following are the examples of docker exec command:
1. Run a Command in a Container
docker exec <container_name> ls /app2. Run an Interactive Bash Shell
docker exec -it <container_name> /bin/bash3. Run a Command as a Different User
docker exec -u <username> <container_name> whoamiThe docker exec command is used to know the container that is running. So to execute the command you need a container which is already running if any container is running in the docker you can execute the command on that container.
docker ps docker run <Name of the image> docker run -d -p 80:80 <image_name>To execute the docker exec command you need to know the <name of the container> or <container ID> which is already running or the container that you have ruined for the test purpose.
docker rename <Present name of container> <New name of the Container>
Here, we are renaming my exist running container with exciting_chandrasekhar with new name as mycontainer1.
docker rename condescending_panini test_imageThe main reason behind to interact with the shell container is to to do some debugging like in the production servers you may face issues like the docker container was restarting automatically for certain interval of time in that case you can interact with the docker container shell and you perform the debugs.
The Second scenario can be you want to install some packages in that application which is running inside the docker container in that case you can interact directly with the container shell and perform the installation which are required.
docker exec -it <Name of the Container> Or <Container ID> bin/bash๐ Docker Exec CommandIn the above example you are going to interact with the docker container shell. In some cases there is no need of interacting with the docker container you can directly install or perform the debugging operations just bu running the following command.
docker exec <name of the container> <Operation which you want to perform> docker exec mycontainer dateIn some use cases like while downloading any type of software or package they will use the /opt directory. Instead of changing to that directory manually each time, you can run commands directly in the desired directory using the docker exec command with the --workdir option.
docker exec --workdir /opt <Name of the container> pwddocker exec --workdir /opt mycontainer pwdRunning the commands as a different user is one of the best practices which is followed by everyone in the production server. The reasons are mentioned below.
Docker community prefers this practice, Use the following command to run the docker commands as an another user
docker exec --user ec2-user <Name of the container> yum updateThe following command will run the docker command in the docker container as an ec2-user and it will update the yum package.
docker exec --user myuser mycontainer apt updateEnvironmental variable in the docker are used to perform the configurations in the docker container instead of hard coding the values you set them as an variable so you can pass them or changes them according to the requirements. Follow the steps mentioned below to pass the environmental variable of docker container.
The following are the steps that guides on how to set the env variables for a docker container:
Step 1: Use the "-e flag" to set the environmental variable as show below.
docker exec -e <Name Of The Variable>: <Value Of the variable> Step 2: Use the env command at the end of the above command it will print outs the all the environmental variables in the docker container. As shown below
docker exec -e <Name Of The Variable>: <Value Of the variable> envdocker run -dit -p 8080:80 --name mywp -e WORKPRESS_DB_HOST=mydb \๐ Creating-wordpress with environmentable variables
-e WORDPRESS_DB_USER=kabali -e WORDPRESS_DB_PASSWORD=redhat \
-e WORDPRESS_DB_NAME=mydb wordpress:latest
No need to access the docker container each and every time when you want to run the command inside the docker container. You can run the docker container command from the outside of the docker container so it will helps you to inspect the docker container and run the docker command at the same time for that you can use the following command.
docker exec <Name of the container> cat /file.txtAcessing the running container shell will give you full command on the container it will be like executing the commands in the virtual machine to interact with the container shell you can use the following command
docker exec -it <Name of the container /bin/bashWhen using the `docker exec` command, you may run into some frequently encountered issues:
Error: No such container: container-name
This error indicates that Docker cannot find the specified container, which may be due to a typo or the container not being active. To resolve this, you can run docker ps to list all running containers and confirm the correct container name.
Error response from daemon: Container container-ID is not running
This message means that while the container exists, it is currently stopped. You can restart the container by executing the command docker start container-name.
Error response from daemon: Container container-name is paused, unpause the container before exec
This error occurs when the container is paused. To continue, you must first unpause the container by using the command `docker unpause container-name`. This will allow you to proceed with executing further commands.
docker exec for Efficient Container ManagementTo take full advantage of docker exec, you can explore advanced use cases to improve your workflow and container management:
You can execute background tasks or long-running processes inside a container without needing to attach an interactive terminal:
docker exec container_name some_process &Use docker exec to attach to a running process inside a container. This is useful for attaching to a debug session or ongoing script execution.
Check for system resources like CPU and memory consumption using commands like top or htop:
docker exec -it container_name topThis helps you monitor the container's performance in real-time without accessing the host machine.
Quickly apply configuration changes or run scripts inside a running container. For example:
docker exec -it container_name bash -c "echo 'New Config' > /etc/config/file"The following are the difference between docker run and docker exec:
| Feature | docker run | docker exec |
|---|---|---|
| Purpose | Creates and starts a new container | Runs a command in an already running container |
| Container State | Starts a new container instance | Requires an existing running container |
| Use Case | Initial setup, launching new applications | Performing tasks or commands in active containers |
| Resource Allocation | Allocates resources and applies configurations | Utilizes existing container resources |
| Command Example | docker run ubuntu:latest /bin/bash | docker exec -it <container_name> /bin/bash |
Article | Link |
|---|---|
Install Docker on Ubuntu | |
Install Docker on Windows | |
Install Docker on Mac OS |
In conclusion, the `docker exec` command is mostly used for providing a seamless interaction with running containers. It is useful in troubleshooting the issues, performing the maintenance tasks and for conduction of security audits. `docker exec` provides the ability to enter inside the container environment and to execute the commands effortlessly. Mastering this command enhances control over container applications working more efficient and streamlined container management workflow.