Docker

Docker run Options

docker run” command is a most essential and robust command line utility of the Docker environment that is used to create and execute the containers using Docker images. Docker “run” command supports different options that are widely used to perform different tasks along with executing the Docker containers as the “–rm” command removes the container after execution, “–mount” is used to bind the external file system to containers and so on.

This write-up will demonstrate:

What are Docker “run” Options?

The following is a list of the options available when using the “docker run” command:

Option Description
–add-host It is utilized for adding a host to the IP mapping.
–attach, -a This option is used to attach the container to the terminal’s standard input, standard output, and standard error.
–cap-add It is utilized to add the Linux capabilities.
–cgroup-parent It assigns a parent group for Docker containers.
–cidfile It is used to add the id of the container into the specified file.
–cpu-count It is a Windows option used to check CPU count.
–detach, -d It is utilized to run the container as a backend service or in detached mode.
–device It assigns the host’s device to the Docker container.
–dns It is utilized to add a custom DNS server.
–dns-search It is used to search the DNS domain.
–domainname It specifies the NIS domain name of containers.
–entrypoint It overwrites or replaces the default entrypoint of the Docker image.
–env, -e It is used to set the environment variables.
–expose It is utilized to specify the exposing port for the container.
–group-add This option specifies the additional groups for containers.
–help It is utilized to view all available options of “run” commands.
–hostname, -h It is used to specify the hostname of containers.
–interactive, -i It keeps the standard input stream open and runs the container interactively.
–init It executes the init within a container.
–ip Specify the ip v4 address.
–kernel-memory Specify the limit of kernel memory.
–label, -l Label the container or metadata for containers.
–link Add a link of the container to another container.
–log-driver Specify the container’s logging drivers.
–memory, -m Specify the limit of memory.
–mount Used to mount volume or file system to containers.
–name It is used to name the container.
–network It is used to attach the network to the container.
–pid Specify the namespace for Process ID.
–platform It is utilized to set the platform for a container for multi-platform containers.
–privileged Execute the containers in privileged mode or assign host privileges to containers.
–publish, -p It is utilized to publish the port on the host.
–pull It is utilized to pull images before running the container.
–restart Adds the restart policy to containers when the container stops.
–rm This option is used to remove the container automatically when the container stops.
–security-opt Specify the security options.
–stop-signal Add the signals to stop the executing containers.
–tty, -t It is used to assign the TTY-pseudo terminal to containers.
–ulimit Specify the Ulimit option. Ulimit limits resource utilization.
–user, -u Specify the user ID or User name of the container.
–volume, -v It is utilized to bind the volume to the container.
–workdir It is used to specify the working directory of the container.

How to Utilize “docker run” Options?

The “docker run” command utilizes different options to behave differently and execute containers along with the different tasks. To utilize the different “docker run” command options, first, list down all supported options with the help of the “–help” option as shown below:

> docker run --help

From the below output, you can view all “docker run” supported options along with their relevant description:

After that, utilize the “docker run” options to execute containers in different manners.

To use the “docker run” command options, go through the below-provided examples.

Example 1: Run Container in Privileged Mode

In order to operate the Docker container with host privileges, check out the below-given command:

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

In the above command, the following options are used:

  • privileged” flag is utilized to allocate the host privileges to the container
  • -d” is used to execute the container in the background
  • -p” is utilized to specify the exposing port for the container:

Example 2: Execute Container in Interactive Mode

To execute the container interactively, the “-i” option is used. Moreover, the “-t” flag allocates the pseudo-TTY terminal to the container:

> docker run -it docker-image

Example 3: Remove Container After Execution

To remove the container automatically after execution, the “docker run” command is used along with “–rm” command:

> docker run --rm golang:alpine

Example 4: Mount Volume to Container

To mount the volume or external file system to the container, follow the provided command:

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

Here:

  • –name” is used to set the container name.
  • –mount” is utilized to mount or bind the volume.
  • source” variable specifies the source volume.
  • target” variable specifies the container’s targeted path:

That’s all about the “docker run” options.

Conclusion

Docker “run” command is a frequently used command to create and execute containers. It supports various options to execute the container in different manners, such as “–name” specifies the name of the container, “–mount” is used to mount the volume, “–rm” removes the container automatically, and many more. This write-up has elaborated on Docker “run” options and how to use them.

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.