Docker has completely changed how we create, distribute, and manage applications. Docker’s lightweight containerization technology makes it possible for programmers to build isolated, repeatable environments. The ability to run commands within Docker containers is a crucial component. Docker exec, a feature that enables easy contact with running containers. To improve the safety, implementation, and compatibility docker image and container should be updated regularly.
It is a crucial tool for troubleshooting, debugging, and other administrative duties. To help you effectively utilize this powerful functionality. We will study the ins and outs of using docker exec to run commands in Docker containers. Developers may design and manage portable, consistent Linux containers with the aid of the containerization program Docker.
Getting a Test Container Going
You require a Docker container to use the docker exec command. Start a test container, if you don’t already have one, by using the docker run command:
docker run -d --name container-name alpine watch "date >> /var/log/date.log"
This command creates a fresh Docker container using the official Alpine image. This well-known Linux container image runs Alpine Linux, a thin, stripped-down operating system version.
The container is separated from our terminal and operated in the background using the -d switch. The container will be named via the command –name container-name. You can enter any name here, or you can leave it blank to let Docker come up with a unique name for the new container on its own.
Alpine comes next, and it specifies the picture we want to use as the container. The last step is to run “date >> /var/log/date.log”. We want to execute this command within the container. The order you give the watch will be executed repeatedly every two seconds by default.
$ Output Fri Jul 23 14:57:05 UTC 2021
The command’s >> /var/log/date.log clause appends the date output to the file /var/log/date.log after redirecting it from the date. A new line will be added to the file every two seconds, and after a few seconds it will resemble this:
How to Find a Docker Container’s Name
The name (or container ID) of the container we wish to work with must be given to the docker exec. The docker ps command can be used to discover the following data:
docker ps
This command displays a list of every Docker container currently active on the server along with some summary data about each one:
The container ID and name are highlighted in this instance. To instruct the docker exec which container to utilize, you can use either. Utilize the docker rename command to rename your container:
docker rename container-name new-name
We’ll then go over various instances of how to use docker exec to run commands inside of an active Docker container.
Using a Docker Container to Run an Interactive Shell
Use docker exec with the -i and -t parameters to launch an interactive shell inside a Docker container so that you may examine the filesystem or troubleshoot running processes.
The -t flag provides a pseudo-terminal that the shell can attach to, and the -i flag maintains input accessible to the container. These flags can be combined in the following way:
docker exec -it container-name sh
This will launch the sh shell in the chosen container and present you with a straightforward shell prompt. Enter the word “exit” and hit ENTER to return to the container:
/ # exit
You could swap out sh for bash in the example above if your container image provides a more sophisticated shell like bash.
Using a Docker Container to Run a Non-Interactive Command
Use the docker exec command without any flags to execute a command within a running Docker container without requiring any interaction:
docker exec container-name tail /var/log/date.log
The container-name container will be given the tail /var/log/date.log command, which will print the results. The last ten lines of a file are printed out by default when using the tail command. You will see if you are operating the demonstration container that we set up in the first part
This is equivalent to running the tail /var/log/date.log command after opening an interactive shell for the Docker container (as was done in the previous stage with docker exec -it container-name sh). However, this method gives the same output in a single command without creating a pseudo-terminal, as opposed to opening a shell, running the command, and then shutting the shell.
Using a Docker Container to Run Commands in a Different Directory
Use the –workdir flag to indicate the directory where a command should be run in your container:
docker exec --workdir /tmp container-name pwd
The following example executes the pwd command to display the current working directory after setting the /tmp directory as the working directory:
The working directory is indeed /tmp, according to the pwd command.
Using a Different User to Run Commands in a Docker Container
Add the –user parameter to execute commands inside your container as a different user:
docker exec --user guest container-name whoami
The whoami command will be executed in the container using the guest user in this case. The current user’s username is printed out by the whoami command:
The current container user is a guest, according to the whoami command.
Environmental Variables Passed to a Docker Container
Environment variables may occasionally need to be passed into a container along with the command to execute. With the -e flag, you can specify an environment variable:
docker exec -e TEST=sammy container-name env
This command executes the env command inside the container after setting the TEST environment variable to equal sammy. After that, the env command displays a list of all the environment variables:
The value of the TEST variable is sammy. Repeat the -e flag for every variable you want to set:
docker exec -e TEST=sammy -e ENVIRONMENT=prod container-name env
You can use the –env-file flag to pass in a file containing environment variables. First, use a text editor to create the file. You can use whichever editor you feel familiar with, however in this case we’ll start a new file in nano:
nano .env
Since it’s normal practice to utilize these files to manage data outside of version control, we’re utilizing the filename extension.env. One per line, enter your KEY=value variables in the file as follows:
Save the document, then exit. Press CTRL+O, ENTER, and CTRL+X to save the file and leave nano, respectively. Docker exec should now be executed with the right filename following –env-file:
docker exec --env-file .env container-name env
The two variables are set in the file. You can define the number of files by using a number of –env-file flags. Whichever file was listed last in the command will take precedence over the earlier files if the variables in the files overlap.
Final Thought on Using Docker Exec to Run Commands in a Docker Container
The docker exec command is a powerful tool for executing actions within Docker containers and easily getting access to their file systems and surroundings. In this blog post, we covered the ins and outs of effectively using docker exec. Hopefully, that would have cleared your doubts, and if you also want to know in detail about how to Install Docker in Ubuntu.
We began by comprehending the use of docker exec and its advantages, including administrative, debugging, and troubleshooting duties within containers. The command’s fundamental syntax was then examined, along with a number of parameters that could have improved its functionality.