Updated March 30, 2023
Introduction to Exec into docker container
In the docker environment, we are running multiple containers. If the containers are running in the background mode, then it will be very difficult to run the external commands on top of the running docker containers. To avoid such conditions, we are having the exec functionality in the docker environment. Thus, we are able to trigger the new command on the running docker container. But while running the command on top of the running the docker container, the new command will start using the docker exec functionality. It will only run when the docker container primary process id should be 1 only, and it should be in running state only, i.e. ( PID 1 ). If the docker container is restarted, then it should not be restarted.
Syntax of Exec into docker container
Given below is the syntax mentioned:
docker exec [ OPTIONS ] [ CONTAINER / CONTAINER ID ] [ COMMAND ] [ ARG... ]
- docker: We can use the docker keyword in the syntax or command. It will accept different arguments like exec, different types of options, container id, current environment level commands, different arguments, etc. As per the provided input, the exec command will be able to run the command on the running docker container as per the provided inputs.
- exec: The exec is the keyword that we need to use with the docker command. It will help to run the command on the running docker container.
- OPTION: As per the requirement, we can provide the different flags as the option that is compatible with the docker command.
- CONTAINER: In the exec command, we can define the container details like the container ID. So, it will identify the running container in the docker environment and trigger the command that you want to execute on the docker container.
- COMMAND: As per the requirement, we can define the command in the exec command. Make sure the command will be valid to run on the container.
- ARG: As per the need or the requirement, we can also use the respective arguments in the exec command.
How Exec into docker container Works?
It will be very difficult to run the external command on top of the running docker container. If the container is already in a running state (in the background). To run the command on the running docker container, we need to use the special keyword, i.e. exec. It is also known as the executable command option. To run the command, we need to define the running command in a specific order or follow the specific syntax.
In the same, we need to define the multiple keywords or the options like exec, different options, container id, commands (that need to be executing on top of the running docker container), different types of arguments, etc. Then, as per the provided inputs, the docker exec command will consider the different options and the arguments. Then, it will take the command which is available in the docker exec command and run on the specific running container (the container ID that we have maintained the exec command).
Below is the list of options available that would be used with the exec command.
Sr. No | Option Name | Shorthand | Default Value | Description |
1 | –detach | -d | NA | It will help for the detached mode. It will help to run the command in the background. |
2 | –detach-keys | NA | It will help to override the key sequence. Then, it will override the current key sequence. Finally, it will help to detach the docker container. | |
3 | –env | -e | NA | The –env option help to set the variable. It will also set the environment variable in the docker environment. |
4 | –env-file | NA | Normally, we can define the environment variable in the runtime, or we can define it. But in some case, if we need to access or call the environment variable from the file. Then we can consider the –env-file variable. | |
5 | –interactive | -i | NA | It is an interactive option. It will keep the STDIN open, even though if it is not in the attached mode. |
6 | –privileged | NA | It will help to give extended privileges to the command. | |
7 | –tty | -t | NA | It will help to allocate the terminal. It will allocate a pseudo-TTY. |
8 | –user | -u | NA | It will help to define the user name or the UID. It will follow the specific format like < User name | uid > [ :<group | gid > ]. |
9 | –workdir | -w | NA | It will help to define the working directory in the docker container. |
Examples of Exec into docker container
Different Examples are mentioned below:
Example #1
Exec into docker container: List the files on running docker container.
In the docker environment, we are able to list the file contents on the running docker container.
Code:
docker exec 7e20c58dcd17 ls /
Explanation :
- As per the above command, we are running the “ls” command on the running docker container, i.e. “7e20c58dcd17”.
Output:
Example #2
Exec into docker container: Create the file on the running docker container.
In the docker environment, we are able to create the new file on the running docker container.
Code:
docker exec 7e20c58dcd17 touch /test_file
Explanation:
- As per the above command, we are creating the “test_file” on the running docker container, i.e. “7e20c58dcd17”.
Output:
Example #3
Exec into docker container: List the number of process on the running docker.
In docker, we are able to list out the number of the running processes. Here, we need to use the exec command utility.
Code:
docker exec 7e20c58dcd17 ps
Explanation:
- As per the above command, we are using the exec command. With the help of the exec command, we are running the “ps” command on top of the running docker container, i.e. “7e20c58dcd17”.
Output:
Conclusion
We have seen the uncut concept of the “Exec into docker container” with the proper example, explanation and command with different outputs. The exec command is used to run the external command on top of the running docker container. So it will help to trigger the command from external environment.
Recommended Articles
This is a guide to Exec into docker container. Here we discuss the introduction, how exec into docker container works? and examples. You may also have a look at the following articles to learn more –