The docker run
command is one of the most powerful and commonly used commands in Docker. It allows you to create and start a container based on a Docker image. This guide will provide a detailed overview of the docker run command, including various options and examples to help you make the most of Docker in your development and deployment processes.
What is the docker run Command?
The docker run command creates and starts a new container from a specified image. It provides numerous options to configure the container's behavior, such as setting environment variables, mounting volumes, and defining network settings.
Basic Syntax
The basic syntax for the docker run command is:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Basic Usage
Let's start with a simple example of running a container based on the hello-world image:
docker run hello-world
This command downloads the hello-world
image (if not already present) and runs it in a new container, displaying a welcome message.
Running Interactive Containers
To run a container interactively, use the -it
options:
docker run -it ubuntu
This command starts a new Ubuntu container and opens an interactive terminal session inside it.
Running Containers in Detached Mode
To run a container in the background, use the -d
option:
docker run -d nginx
This command starts an Nginx web server container in detached mode, allowing it to run in the background.
Mapping Ports
To map a container's internal ports to your host machine, use the -p
option:
docker run -d -p 8080:80 nginx
This command maps port 80 inside the Nginx container to port 8080 on your host machine, making the web server accessible at http://localhost:8080
.
Mounting Volumes
To mount a directory from your host machine into a container, use the -v
option:
docker run -d -v /host/path:/container/path nginx
This command mounts the /host/path
directory from your host machine into the /container/path
directory inside the Nginx container.
Setting Environment Variables
To set environment variables inside a container, use the -e
option:
docker run -d -e MY_ENV_VAR=value ubuntu
This command sets the MY_ENV_VAR
environment variable to value
inside the Ubuntu container.
Removing Containers After Exit
To automatically remove a container after it exits, use the --rm
option:
docker run --rm hello-world
This command runs the hello-world
container and removes it automatically after it finishes.
Naming Containers
To assign a name to a container, use the --name
option:
docker run -d --name my_nginx nginx
This command names the Nginx container my_nginx
, making it easier to reference.
Running Commands Inside Containers
To run a specific command inside a container, simply add the command and its arguments at the end of the docker run
command:
docker run ubuntu ls -la
This command starts an Ubuntu container and runs the ls -la
command inside it.
Conclusion
The docker run
command is a versatile tool that allows you to create and manage containers with ease. By understanding and utilizing its various options, you can effectively control the behavior and environment of your Docker containers. Whether you're running simple applications or complex multi-container setups, mastering the docker run
command is essential for any Docker user.