Docker

Different Ways to Use “docker run” Command

In the Docker platform, the “run” is the most basic and robust command widely utilized to execute the image to build and execute the Docker containers. More specifically, the “docker run” command supports various options to create and fire up the container in different manners, 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 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 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:

> docker run -it docker-image

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:

$ docker run --privileged -d -p 8080:8080 golang:alpine

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:

> docker run -d --name go-container --mount source=my-golang-vol,target=/app golang:alpine

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:

> docker run --rm golang:alpine

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:

> docker run -d --net=host go-img

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.