2022-06-10

Docker Run Command

Docker Run Command

The docker run command is a fundamental part of working with Docker, allowing users to create and start new containers from images. In this article, I will dive deep into the various command flags that can be used with the docker run command to customize container behavior.

Basic Syntax and Usage

The basic syntax for the docker run command is as follows:

bash
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

OPTIONS are the command flags that we will be discussing in this article. IMAGE is the name of the Docker image, while TAG and DIGEST are optional identifiers. COMMAND and ARG are additional commands and arguments to execute inside the container.

Essential Docker Run Command Flags

-d, --detach

This flag is used to run the container in detached mode, meaning it runs in the background and does not require the terminal to remain open. To use this flag, simply include -d or --detach in your docker run command.

bash
$ docker run -d nginx

-it (Interactive and TTY)

The -it flag is a combination of two separate flags, -i (interactive) and -t (TTY). Using these flags together enables you to interact with the container in real-time and allocate a pseudo-TTY, allowing for a more interactive experience when running containers.

The -i or --interactive flag keeps the container's standard input (stdin) open even if it's not attached to the terminal. This allows you to send input to the container while it's running.

The -t or --tty flag allocates a pseudo-TTY for the container. This emulates a text terminal, making it easier to interact with the container's command line interface.

bash
$ docker run -it ubuntu bash

In this example, we start an ubuntu container with an interactive Bash shell. The -it flag allows you to execute commands within the container and receive output in real-time, just as if you were working with a local terminal.

-p, --publish

The -p or --publish flag is used to map a container's port to the host machine's port. This is essential for exposing container services to external networks or the host machine itself.

bash
$ docker run -d -p 8080:80 nginx

-v, --volume

The -v or --volume flag is used to mount a host directory or a named volume to a container's file system. This is useful for persisting data and sharing files between the container and host machine.

bash
$ docker run -d -v /path/on/host:/path/in/container nginx

-e, --env

The -e or --env flag allows you to set environment variables within the container. This is useful for passing configuration information to the application running inside the container.

bash
$ docker run -d -e MYSQL_ROOT_PASSWORD=my-secret-password mysql

--name

The --name flag assigns a custom name to the container, making it easier to manage and reference.

bash
$ docker run -d --name my-nginx-container nginx

--restart

The --restart flag determines the container's restart policy in case of a failure or when the host machine reboots. It accepts one of the following values: no, on-failure, unless-stopped, or always.

bash
$ docker run -d --restart=always nginx

--memory

The --memory flag limits the amount of memory a container can use. This is useful for controlling resource usage on the host machine and preventing any single container from consuming excessive resources.

bash
$ docker run -d --memory=512m nginx

--cpu-shares

The --cpu-shares flag assigns relative weights for CPU usage between containers. This allows you to control how much CPU time each container receives when multiple containers are competing for resources.

bash
$ docker run -d --cpu-shares=1024 nginx

--network

The --network flag connects the container to a specific network. You can use this flag to control the networking environment of your container and facilitate communication between containers.

bash
$ docker run -d --network=my-custom-network nginx

--rm

The --rm flag automatically removes the container once it exits. This is useful for keeping your host machine clean and free from unnecessary clutter, especially when running short-lived containers.

bash
$ docker run --rm -it ubuntu bash

--entrypoint

The --entrypoint flag allows you to override the default entrypoint of the container, which is the command that is executed when the container starts. This is useful for running different commands or executables within the container without having to create a new Docker image.

bash
$ docker run --entrypoint=/bin/sh -it nginx

In this example, we override the default entrypoint of the nginx container to execute /bin/sh instead. This allows us to access the container's shell interactively.

References

https://docs.docker.com/engine/reference/commandline/run/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!