Docker is a platform that uses virtualization and allows users to develop, test, and deploy applications as portable containers. To get started and work with Docker, an individual has to use several commands. One of the most basic and important commands amongst them is the docker run command. While being utilized for various actions, its primary function is to build and run containers.
An individual utilizes different syntaxes (covered in the blog) along with various attributes to perform actions on Docker like adding a custom host, add a name, run in detached mode, etc. The blog contains all the information regarding the docker run command along with their examples.
Docker Run Command
The standard syntax of the docker run command is discussed below:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The only requirement to create a container from the docker run command is the image name. For this, you can run containers from locally stored Docker images, else the software will pull it from the online registry. You include the name of the image in the command in the following fashion:
docker run [docker_image]
List of Attributes
To perform various actions with the Docker run command, you need to use the following attributes. We’ve listed all the major and frequently-used attributes along with their description.
Attributes | Description |
–runtime | Runtime to use for that specific container |
–restart | Apply restart policy when a container exits |
–rm | Removing containers automatically after exit |
–pull | Pull image before running |
–add-host | Allows you to add a custom host-to-IP |
–cap-add
–cap-drop |
Add Linux capabilities
Drop Linux capabilities |
–detach, -d | Allows container to run in the background |
–detach-keys | Used to detach a container irrespective of the key sequence |
–device | Use to add host device to a specific container |
–device-read-bps
–device-read-iops |
Limits read rate in bytes per second
Limits read rate in IO per second |
–device-write-bps
–device-write-iops |
Limits write rate in bytes per second
Limits write rate in IO per second |
–disable-content-trust | Used to skip image verification |
–env-file | Allows you to read a file of environment variables |
–expose | Used to expose a port or a lot of ports |
-group-add | Includes groups to join |
–publish, -p
–publish-all, -P |
Expose a container’s port(s) to the host
Expose all ports to the random ports |
–security-opt | Displays all the security options |
–health-cmd | Runs a check health |
–health-timeout | Maximizes the time to allow checks to run |
–hostname, -h | Displays the hostname of the container |
–label, -l | Allows you to set metadata to a container |
–link | Liberates to add a link to a container |
–log-driver | Logging driver |
–memory, -m | Displays memory limit |
–mount | Connects a filesystem mount to a specific container |
–name | Allows you to assign a name to the container |
–net | Used to connect a container to a specific network |
–volume, -v | Restricts a specific volume |
–volume-driver | Add an optional volume driver to a container |
–no-healthcheck | Remove any health check that is specified to a container |
–oom-kill-disable | Disable OOM Killer |
–platform | Add platform to a multi-platform capable server |
–workdir, -w | Open the working directory inside the container |
Examples
1. Run in interactive mode
You can run the container in interactive mode, which means you can execute commands inside the container while it is running. The interactive mode allows you to access a command prompt inside the running container.
Run the following command to achieve the same:
docker container run -it [docker_image] /bin/bash
2. Publish specific ports
When a container is running, you can access it only from the inside of it. Therefore, to enable connections from outside the container, you have to publish specific ports.
Add -p option to the docker run command along with add the following options:
-p [host_ip]:[host_port]:[container_port]
However, keep a note that the host_ip element is optional and no specification is required when running the command.
3. Mount host volumes
In Docker, none of the produced data is saved. Right after the process is finalized, the container stops, and data vanishes. However, to save and access the data after the container stops, you need to enable sharing storage volumes.
Therefore, use the -v attribute to mount host volumes with the specified location of the directory to save all your data.
-v [/host/volume/location]:[/container/storage]
4. Removing container after the competition of the process
If you want a container to perform the specified task and leave behind no file afterward, then you can delete the container to get rid of all the data that it contains. Use the following command to run a container that will be automatically removed after the completion of a specified process.
docker container run --rm [docker_image]
5. Enabling detach mode
Start a container in detached mode to keep the container running even after exiting the terminal session. The detached container will only stop after the termination of the root process. Use the following command to start a detached container.
docker container run -d
6. Running a container in the foreground
When instructions are provided to the docker run command on the terminal, the root process gets started in the foreground by default. This simply means that the standard input, output, and error from the root process will be attached with the terminal sessions. Following is the command that will allow running a container in the foreground:
docker container run
7. Remove the container after exit
When a container exits, its file system gets preserved on the host system. By using the –rm attribute, the docker run command will remove the container automatically after it exits. Typically, this option is used on foreground containers that perform short-term tasks. Here’s the command that you need to use:
docker container run --rm
Conclusion
Docker is currently the most popular platform for packaging and deploying applications via containers and has become an integral part of many organizations. With more people using Docker, it becomes important for them to learn about the basic commands, such as docker run commands, that are essential for managing Docker containers.
Since Docker has gained a prominent place in the development community, it’s essential for any individual who is interested in operating the Docker platform to start their journey by mastering the docker run command. In this article, we made sure that you get familiar with the docker run commands along with their popular attributes.