An explanation of 10 commonly used options for the docker run CLI command.
The docker run CLI command has around 100 options, including
-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
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...]
docker runcommand first creates a writeable container layer over the specified image, and then starts it using the specified command. — docker run extended description | Docker Documentation
Similar Docker CLI commands
docker run command is similar to
docker exec, which is to run a command in an existing container.
A stopped container can be restarted with all its previous changes intact using docker start. Use the following command to view a list of all containers.
docker ps -a
Top 10 options
The top 10 docker run options in alphabetical order.
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.
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.
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
Declare an IP address, for example
Assign a name to the container,
--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
-p publishes a container’s port(s) to the host, while
-P publishes all exposed ports.
Automatically remove the container when it exits.
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.
Mount a volume
-v /my_volume. If you are new to volumes then find out more in Docker’s guide to Volumes.
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.