← Back to Docker Mastery
Basic13 min read

Running Containers

Run and manage containers effectively — port mapping, environment variables, process management, and connecting containers together.

docker run Options

docker run creates and starts a container. Essential flags: -d (detached), -p (port mapping), -e (environment variables), -v (volume mount), --name (container name), --rm (auto-remove on stop).

Port mapping format is host:container — -p 8080:80 maps host port 8080 to container port 80. Use -P to publish all exposed ports to random host ports.

  • --restart unless-stopped auto-restarts after crashes or reboots
  • -e accepts KEY=VALUE or --env-file for bulk variables
  • --rm cleans up containers automatically after they exit
docker run -d \
  --name web \
  -p 8080:80 \
  -e NODE_ENV=production \
  -v $(pwd)/data:/app/data \
  --restart unless-stopped \
  nginx:alpine

Process Management

Containers run a single main process (PID 1). When PID 1 exits, the container stops. Use docker stop for graceful shutdown (SIGTERM then SIGKILL after timeout) and docker kill for immediate termination.

Run background processes with a process manager like supervisord, or better — run one process per container and use Docker Compose for multi-service applications.

docker stop web          # Graceful (SIGTERM → wait → SIGKILL)
docker kill web          # Immediate (SIGKILL)
docker restart web       # Stop + start
docker update --restart=always web

Environment Variables

Pass configuration via environment variables with -e or --env-file. Never bake secrets into images — inject them at runtime. The --env-file flag loads variables from a .env file.

Environment variables are the standard way to configure containers for different environments (development, staging, production) using the same image.

# Single variable
docker run -e DATABASE_URL=postgres://db:5432/myapp myapp

# From file
docker run --env-file .env.production myapp

# .env.production
DATABASE_URL=postgres://prod-db:5432/myapp
REDIS_URL=redis://cache:6379

Interactive Containers

Run interactive containers with -it for terminal access. Useful for debugging, database clients, and one-off tasks. Combine with --rm for temporary containers that clean up after exit.

Use docker exec -it for accessing running containers without stopping them. This is the primary debugging workflow for production containers.

# Interactive shell in new container
docker run -it --rm node:20-alpine sh

# Shell into running container
docker exec -it web sh

# Run a one-off command
docker exec web npm run migrate

Resource Constraints

Limit container resources to prevent one container from consuming the entire host. --memory limits RAM, --cpus limits CPU, and --pids-limit caps process count.

Always set resource limits in production. Without them, a memory leak in one container can crash the entire host.

docker run -d \
  --memory=512m \
  --cpus=1.0 \
  --pids-limit=100 \
  myapp:latest

Get In Touch


Ready to discuss your next project? Drop me a message.