The Docker run command is one of the most important and frequently used commands in Docker. It is the primary way to create and start containers from Docker images. Whether you are a beginner learning Docker basics or an intermediate user working with real-world applications, understanding the docker run command is essential.
This guide explains the Docker run command in a clear, structured, and practical way. You will learn its syntax, commonly used options, real-world examples, and best practices that align with modern containerization workflows.
The Docker run command is used to:
In simple terms, docker run combines multiple Docker actions into a single command:
docker run [OPTIONS] IMAGE [COMMAND] [ARGUMENTS]
docker run hello-world
This example:
This is often the first command used to verify a Docker installation.
Interactive containers allow you to interact with a running process, such as a shell.
docker run -it ubuntu
This command starts an Ubuntu container and gives you access to the shell.
Detached mode runs containers in the background.
docker run -d nginx
Port mapping allows external access to services running inside containers.
docker run -d -p 8080:80 nginx
| Host Port | Container Port | Purpose |
|---|---|---|
| 8080 | 80 | Expose Nginx web server |
Now you can access Nginx at http://localhost:8080.
Environment variables are commonly used to configure applications.
docker run -e APP_ENV=production -e DB_HOST=localhost myapp
Volumes persist data beyond the container lifecycle.
docker run -v /host/data:/container/data ubuntu
By default, Docker assigns random container names. You can define your own.
docker run --name webserver nginx
Restart policies help keep containers running.
docker run --restart always nginx
| Docker Run | Docker Start |
|---|---|
| Creates and starts a container | Starts an existing container |
| Used for new containers | Used for stopped containers |
Running a web server like Nginx or Apache.
docker run -d -p 80:80 nginx
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
Quickly spin up isolated environments for testing.
By learning its syntax, options, and real-world applications, you gain full control over how containers are created and managed.
The Docker run command creates a new container from an image and starts it. It can also pull the image if it does not exist locally.
Docker run creates and starts a container, while docker start only starts an existing stopped container.
Use the -d option to run the container in detached mode.
Use the -p option to map host ports to container ports.
Yes, environment variables can be passed using the -e option to configure applications inside containers.
The Docker run command is the foundation of working with Docker containers. From running simple test images to deploying production-ready services, mastering docker run helps you understand container lifecycle, configuration, and runtime behavior.
Copyrights © 2024 letsupdateskills All rights reserved