a quick reference guide for the CLI command
Top 10 options for docker run — a quick reference guide for the CLI command
The docker run
CLI command has around 100 options, including -d
, -i
, -t
, -v
, and -w
. This tutorial will explain 10 of the most common ones, as well as links to learn more about using them. Below is an example of docker run
with the options covered in this article.
docker run \ --rm \ --detach \ --env KEY=VALUE \--ip 10.10.9.75
\--publish 3000:3000 \ --volume my_volume \ --name my_container \ --tty --interactive \ --volume/my_volume
\ --workdir /app
\ IMAGE bash
The docker run CLI command
This creates a container based on an image. It executes your chosen command and arguments in a new container with the configuration provided by your given options.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Thedocker run
command firstcreates
a writeable container layer over the specified image, and thenstarts
it using the specified command. — docker run extended description | Docker Documentation
Similar Docker CLI commands
The docker run
command is similar to docker exec
A stopped container can be restarted with all its previous changes intact using docker start
. Use the command docker ps -a
to view a list of all containers. For more on the key Docker commands, check out Top 10 Docker CLI commands you can’t live without.
Options
The top 10 docker run
options in alphabetical order.
1) --detach, -d
By default a Docker container is run attached to local standard input, output, and error streams. The -d, --detach
option runs the container in the background of your terminal session so its output is not displayed.
2) --entrypoint
Set or overwrite the default entrypoint command for the image. The entrypoint sets the command and parameters that will be executed first when a container is run. Any any commands and arguments passed at the end of the docker run command will be appended to the entrypoint. To learn more about using Entrypoint, check out Docker ENTRYPOINT & CMD: Dockerfile best practices
3) --env, -e
Set an environment variable using a KEY=VALUE
pair. If you have environment variables in file, you can pass in the file path to the option --env-file
.
4) --ip
Declare an IP address, for example --ip=10.10.9.75
5) --name
Assign a name to the container, --name my_container
6) --publish, -p | --publish-all, -P
These publish port mappings between the container and host that are defined in an image’s Dockerfile or by using the expose option, --expose
. The option --publish, -p
publishes a container’s port(s) to the host, while --publish-all , -P
publishes all exposed ports. You can learn more about exposing and defining ports in Expose vs publish: Docker port commands explained simply
7) --rm
Automatically remove the container when it exits. The alternative would be to manually stop it and then remove it, for more on how to do this see: How to delete Docker containers from the command line
8) --tty, -t
Allocate a virtual terminal session within the container. This is commonly used with the option --interactive, -i
, which keeps STDIN open even if running in detached mode. One of the most common uses of -i -t
is to run a command, such as bash
, in a container, which you can read more about in my post Run bash or any command in a Docker container
9) --volume, -v
Mount a volume -v /my_volume
. If you are new to volumes then find out more in Docker’s tutorial What are Data Volumes?
10) --workdir , -w
State the working directory inside the container. For example, if you copy your files into an app
folder within your container then you may want to set the working directory to app
.
Find out more
If you want to see the full list of docker run options, they are listed in Docker’s run CLI documentation along with other useful examples. There are many great Docker tutorials online, such as Docker Con EU’s Hands-On Labs. Also see my other Docker posts below to read more about how to use Docker on Medium.