This write-up will illustrate the different ways to use the “docker run” command.
Different Ways to Use “docker run” Command
There are various options supported by the “docker run” command to execute and manage the Docker containers in different ways. For this purpose, we have listed some uses of the “docker run” command to create and manage containers in different ways:
- How to Execute Containers in Interactive Mode?
- How to Run Containers in Privileged Mode?
- How to Mount Volume With the Docker Container?
- How to Automatically Remove Containers After Execution?
- How to Run a Container on Host Network?
How to Execute Container in Interactive Mode?
To execute the Docker container in an interactive mode, utilize the “-i” option along with the “docker run” command. Similarly, in order to allocate the TTY-pseudo terminal to the container, use the “-t” flag:
How to Execute Container in Privileged Mode?
To execute the Docker container with the root or host capability, utilize the “–privileged” option with the “docker run” command:
In the above code block, the following options are used:
- “–privileged” option executes the container with host privileges.
- “-d” flag runs the container as a backend service or in detached mode.
- “-p” defines the container’s exposed port:
How to Mount Volume With the Docker Container?
Mounting volume means connecting the external file system to the container. To bind the volume with the specified container, go through the provided “docker run” command:
Here:
- “–name” defines the name of the container.
- “–mount” binds the external file system or volume with the container.
- “source” attribute is used to specify the source volume.
- “target” attribute defines the target path of the container:
How to Automatically Remove Container After Execution?
One of the major features of a “docker run” command is to remove the container automatically after the deployment. This technique is widely used in project or application testing processes. To remove the container automatically after execution, simply utilize the “–rm” flag with the “docker run” command:
How to Run a Container on Host Network?
By default, the containers are executed on the bridge network. But sometimes, developers usually want to operate containers on the host. For this purpose, the “–net=host” flag is used to execute the container on the host network. However, you can run the container on any network or custom network through the “–net=<network-name>” option:
We have elaborated on the different ways to utilize the “docker run” command in Docker.
Conclusion
The “docker run” is a famous command line utility of Docker that supports numerous options to create and run the containers in different ways, such as “-t” allocated the TTY-pseudo terminal to the container, “–rm” removing the container automatically after execution, and “-i” is utilized to operate the container interactively. This article has provided the different uses of the “docker run” command to operate containers in different manners.