Docker Run Command Examples

docker run creates and starts a container from an image. Mastering its flags — for port mapping, volume mounts, environment variables, naming, and resource limits — gives you full control over how containers run in both development and production environments.

DockerRunDevOpsCLI
Basic Run
Run a container interactively
docker run -it ubuntu bash
-i keeps stdin open and -t allocates a pseudo-TTY. Together they give you an interactive shell inside the container. When you exit the shell, the container stops. Use this for debugging and exploration.
Run in detached mode (background)
docker run -d nginx
The -d flag runs the container in the background and prints the container ID. The terminal is immediately returned to you. Use docker logs container_id to view output and docker stop container_id to stop it.
Name the container
docker run -d --name mywebserver nginx
The --name flag assigns a human-readable name instead of a random one. Named containers are easier to reference in logs, stop, and exec commands: docker stop mywebserver instead of a long container ID.
Auto-remove when stopped
docker run --rm alpine echo "Hello World"
The --rm flag automatically removes the container when it exits. Ideal for one-off commands and CI steps where you don't want containers accumulating. Without this, stopped containers remain until manually removed.
Ports & Volumes
Map a host port to container port
docker run -d -p 8080:80 nginx
The -p host:container flag maps a port on your machine to a port inside the container. This makes nginx accessible at http://localhost:8080. The container port (80) stays fixed; change the host port (8080) as needed.
Mount a host directory as a volume
docker run -d -v /host/path:/container/path nginx
The -v host:container flag mounts a host directory into the container. Changes made inside the container are immediately reflected on the host and vice versa. Essential for development workflows where you edit files locally.
Mount as read-only
docker run -d -v /host/config:/etc/config:ro nginx
Append :ro to make the volume read-only inside the container. The container can read the files but cannot modify them. Use this for config files and secrets to prevent accidental modification by the containerized process.
Environment & Resources
Set environment variables
docker run -d -e NODE_ENV=production -e PORT=3000 myapp
The -e flag sets environment variables inside the container. Use multiple -e flags for multiple variables. The running process can read these like any other environment variable in its language.
Load environment from a file
docker run -d --env-file .env myapp
The --env-file flag loads environment variables from a file. Each line should be in KEY=VALUE format. This is cleaner than multiple -e flags and keeps secrets out of shell history.
Limit CPU and memory
docker run -d --cpus="1.5" --memory="512m" myapp
--cpus limits CPU usage (1.5 = one and a half cores). --memory limits RAM. Setting resource limits prevents a single runaway container from starving the host or other containers. Essential for production and shared environments.
Connect to a specific network
docker run -d --network mynetwork myapp
The --network flag attaches the container to a named Docker network. Containers on the same network can communicate using container names as hostnames. Create a network first with docker network create mynetwork.

How to Use

  1. Start with docker run image-name to run a container interactively.
  2. Add -d to run in the background and --name to give it a recognizable name.
  3. Map ports with -p host:container and mount volumes with -v host:container.
  4. Inject configuration with -e KEY=VALUE or --env-file .env.
  5. Use --rm for short-lived commands to avoid container accumulation.

Frequently Asked Questions

What is the difference between docker run and docker start?

docker run creates a new container from an image and starts it. docker start restarts a container that was previously created and stopped. Use docker run for new containers and docker start to restart existing ones.

How do I get a shell in a running container?

Use docker exec -it container_name bash (or sh for Alpine-based images). Unlike docker run -it, exec connects to an already-running container without starting a new one. This is the standard way to inspect or debug a live container.

Why does my container stop immediately after starting?

Containers run only as long as their main process (PID 1) is running. If your CMD or ENTRYPOINT exits immediately, the container stops. Common causes: the process runs in the background (use -d with the foreground command), or there's a startup error (check docker logs container_id).

Related Tools