1.03 Docker Run
Docker run is one of the most important Docker commands. It creates and starts a container from an image. This page covers common docker run options used in real projects: tags, interactive input, port mapping, volume mapping, inspection, and logs.
Goal
Understand how to run containers in a predictable way for development, testing, and production-like environments.
Why docker run Matters
A Docker image is a packaged template. A container is a running instance of that image.
Example:
If the image is not available locally, Docker pulls it from the registry first, then starts the container.
Note
If no image tag is specified, Docker uses the latest tag by default.
Run With Image Tags
Tags allow you to choose a specific version of an image.
In the first command, Docker uses:
In the second command, Docker uses:
Avoid latest in production
Do not depend on latest for production deployments. The latest tag can change over time, which may cause unexpected version changes.
Production practice
Pin image versions explicitly.
Better for production:
Run in Interactive Mode
Some containers need user input. By default, Docker containers do not listen to standard input.
Example application behavior:
Output:
If you run the same application in Docker without interactive mode:
It may not wait for input.
Use -i for interactive mode:
Use -t to allocate a pseudo-terminal:
Best option for interactive containers
Use -it together when you need to type input inside the container.
Run With Port Mapping
Containers have their own internal network. If an application listens inside the container, external users cannot access it unless you publish the port.
Example web app running inside the container on port 5000:
The application may listen on:
But that is inside the container network.
Use -p to publish a container port on the Docker host:
Format:
So this means:
Now users can access the app using:
Multiple containers with different host ports
You can run multiple containers from the same image by mapping them to different host ports.
docker run -p 8000:5000 kodekloud/webapp
docker run -p 8001:5000 kodekloud/webapp
docker run -p 8002:5000 kodekloud/webapp
Common mistake
You cannot bind multiple containers to the same host port at the same time.
This will fail if port 80 is already in use:
Run With Environment Variables
Many application images require configuration through environment variables.
Example MySQL container:
Format:
Tip
Use environment variables for non-sensitive runtime configuration. For secrets, prefer Docker secrets, Kubernetes Secrets, or an external secrets manager in production.
Run With Volume Mapping
Containers have their own filesystem. If a container is deleted, data inside the container is also deleted.
For databases like MySQL, data is commonly stored inside:
If you run MySQL without a volume:
Then stop and remove it:
The container data is lost.
Use -v to map a host directory to a container directory:
Format:
This means:
Why volumes are important
Volumes help persist application data even after the container is removed.
Production storage
For production databases, do not rely only on local host paths without backup, monitoring, and recovery planning.
Run in Detached Mode
By default, some containers run in the foreground. You stay attached to the container output.
Use -d to run in the background:
Docker returns the container ID immediately.
Check running containers:
Attach back to the container:
Note
You can use the first few characters of the container ID as long as they uniquely identify the container.
Inspect a Container
Use docker inspect to view detailed container information in JSON format.
This shows details such as:
- container state
- image configuration
- command and arguments
- mounts
- network settings
- environment variables
Inspect container
Useful fields:
Production troubleshooting
Use docker inspect when you need exact details about ports, mounts, IP address, environment variables, or restart behavior.
View Container Logs
If a container runs in detached mode, use docker logs to view its output.
Follow logs live:
Show recent logs:
Show timestamps:
When logs are empty
If docker logs shows nothing, the application may be writing logs to a file inside the container instead of standard output.
Production logging
Containerized applications should write logs to stdout and stderr. This makes logs easier to collect using Docker, Kubernetes, Fluent Bit, CloudWatch, Loki, or other logging systems.
Common docker run Options
| Option | Meaning | Example |
|---|---|---|
-d |
Run in detached/background mode | docker run -d nginx |
-i |
Keep STDIN open | docker run -i ubuntu |
-t |
Allocate pseudo-terminal | docker run -it ubuntu bash |
-p |
Publish port | docker run -p 80:5000 webapp |
-v |
Mount volume | docker run -v /data:/var/lib/mysql mysql |
-e |
Set environment variable | docker run -e APP_ENV=prod app |
--name |
Assign container name | docker run --name web nginx |
--rm |
Remove container after exit | docker run --rm alpine echo hi |
Practical Examples
Stores MySQL data on the Docker host.
Production Best Practices
Do
- Pin image versions instead of using
latest. - Use
--namefor important containers. - Use
-dfor long-running services. - Use
-ponly for ports that must be exposed. - Use volumes for persistent data.
- Send logs to
stdoutandstderr. - Store secrets securely instead of hardcoding them in commands.
- Use health checks and restart policies for long-running workloads.
Don't
- Do not run databases without persistent storage.
- Do not expose unnecessary ports.
- Do not use random image tags in production.
- Do not store passwords directly in shell history for real environments.
- Do not manually run production containers without a repeatable deployment process.
- Do not depend only on container logs without centralized logging.
Recommended Production-Style Command
docker run -d \
--name webapp \
--restart unless-stopped \
-p 8080:8080 \
-e APP_ENV=production \
kodekloud/simple-webapp:1.0
Explanation:
-druns the container in background mode.--namegives the container a clear name.--restart unless-stoppedrestarts the container after failures or host reboot.-pexposes the application port.-esets runtime configuration.- The image uses a specific version tag.
Warning
For real production, prefer orchestration platforms like Kubernetes, Docker Compose, ECS, or Nomad instead of manually running many containers with raw docker run.
Quick Troubleshooting
| Problem | Check |
|---|---|
| Container exits immediately | docker ps -a, docker logs <container> |
| App not accessible | Check -p HOST_PORT:CONTAINER_PORT |
| Port already allocated | Use a different host port |
| Data disappeared | Check if a volume was used |
| Wrong app version | Check image tag |
| Need full container details | docker inspect <container> |
| Need live logs | docker logs -f <container> |
Quick Command Summary
# Run latest Redis
docker run redis
# Run specific Redis version
docker run redis:4.0
# Run interactive container
docker run -it ubuntu bash
# Run web app with port mapping
docker run -p 80:5000 kodekloud/webapp
# Run container in background
docker run -d kodekloud/simple-webapp
# Run MySQL with persistent storage
docker run -v /opt/datadir:/var/lib/mysql mysql
# Inspect container
docker inspect <container-name-or-id>
# View logs
docker logs <container-name-or-id>
# Follow logs
docker logs -f <container-name-or-id>
Remember
docker run is not only for starting containers. It also defines how the container connects to input, ports, volumes, environment variables, logs, and the host system.