Stage 4 · Provision
Running & Networking Containers
docker run
Use docker run options for names, detached mode, restart policies, commands, and interactive terminals.
Basic Usage
docker run is the primary command for creating and starting containers. It combines docker create (which sets up the container) and docker start (which runs it). The syntax is docker run [OPTIONS] IMAGE [COMMAND].
# Run a container (foreground — attaches to terminal)
docker run alpine echo "Hello from container"
# Run with a specific image version
docker run nginx:1.25-alpine
# Run and immediately exit (press Ctrl+C to stop)
docker run -it alpine shBy default, docker run runs the container in the foreground. You see the container's output directly in your terminal. Press Ctrl+C to send SIGINT and stop the container.
Detached Mode
Detached mode (-d) runs the container in the background. The command returns the container ID and returns control to your terminal. This is how most production containers run.
# Run in background
docker run -d nginx
# Returns container ID
# a1b2c3d4e5f6...
# Check what is running
docker ps
# View logs of detached container
docker logs <container-id>The -d flag is essential for long-running services. Without it, your terminal is blocked by the container's output. Use docker logs to view output from detached containers.
Naming Containers
By default, Docker generates random names like brave_newton. Explicit names (--name) make containers easier to reference in commands, logs, and scripts. Good naming conventions improve manageability.
# Give the container a name
docker run -d --name web nginx
# Reference by name in other commands
docker logs web
docker exec web cat /etc/nginx/nginx.conf
docker stop web
docker rm web
# Name format: lowercase letters, numbers, hyphens, underscores
docker run -d --name my-api-server-1 nginxContainer names must be unique across all containers on the host. If you try to reuse a name, Docker will refuse to create the container.
Restart Policies
Restart policies control whether Docker restarts a container when it exits. This is critical for production containers that should survive crashes or host reboots.
| Policy | Behavior |
|---|---|
| no | Never restart (default) |
| on-failure[:max] | Restart only on non-zero exit code, up to max times |
| always | Always restart, including on daemon startup |
| unless-stopped | Always restart unless explicitly stopped |
# Restart on failure, up to 5 times
docker run -d --restart=on-failure:5 myapp
# Always restart (survives daemon restarts)
docker run -d --restart=always nginx
# Restart unless explicitly stopped
docker run -d --restart=unless-stopped myappalways and unless-stopped are similar, but unless-stopped remembers that you explicitly stopped the container. When the daemon restarts, unless-stopped containers stay stopped, while always containers restart.
unless-stopped is the best default for long-running services. It restarts on crashes and host reboots, but respects explicit docker stop commands. always can cause confusion when you want to stop a container during maintenance.
Overriding CMD
You can override the image's CMD by appending a command after the image name. This is useful for running one-off tasks, debugging, or changing the startup behavior without modifying the image.
# nginx image default: nginx -g 'daemon off;'
# Override to run a shell instead
docker run -it nginx:alpine sh
# Override to check config
docker run --rm nginx:alpine nginx -t
# Override to list files
docker run --rm nginx:alpine ls -la /etc/nginx/The command after the image name replaces CMD entirely. ENTRYPOINT is not affected. If the image uses ENTRYPOINT, the command becomes an argument to ENTRYPOINT.
Interactive Sessions
Interactive sessions let you type commands inside a running container. The -it flags are required: -i keeps STDIN open, -t allocates a pseudo-TTY. Without both, interactive sessions will not work correctly.
# Open a shell in a running container
docker exec -it mycontainer sh
# Run a new container interactively
docker run -it --rm alpine sh
# Run with a specific user
docker exec -it -u root mycontainer sh
# Attach to a running container's output
docker attach mycontainer
# Detach with Ctrl+P, Ctrl+Q (not Ctrl+C)docker exec starts a new process inside the container. docker attach connects to the container's main process. Use exec for debugging, attach only when you need to interact with the main process.
When using docker attach, Ctrl+C sends SIGINT to the main process and stops the container. To detach without killing the container, use the escape sequence: Ctrl+P then Ctrl+Q.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.