← Back to DevOpsToolbox

Docker

Docker Commands Cheat Sheet

Essential Docker commands for containers, images, logs, networking, volumes and troubleshooting.

Essential Docker Commands

docker --version

Check the installed Docker version.

docker ps

List currently running containers.

docker ps -a

List all containers, including stopped containers.

docker images

List Docker images available locally.

docker pull nginx

Download the nginx image from a container registry.

docker run nginx

Create and start a container from the nginx image.

docker run -d nginx

Run an nginx container in detached mode.

docker run -d -p 8080:80 nginx

Run nginx and map host port 8080 to container port 80.

docker start <container>

Start an existing stopped container.

docker stop <container>

Stop a running container.

docker restart <container>

Restart a container.

docker rm <container>

Remove a stopped container.

docker rm -f <container>

Force remove a running or stopped container.

docker logs <container>

View logs generated by a container.

docker logs -f <container>

Follow container logs in real time.

docker exec -it <container> /bin/bash

Open an interactive Bash shell inside a container.

docker inspect <container>

Display detailed information about a container.

docker stats

Monitor CPU, memory, network and other container resources.

docker build -t myapp .

Build a Docker image from a Dockerfile.

docker rmi <image>

Remove a Docker image.

docker network ls

List Docker networks.

docker volume ls

List Docker volumes.

docker system df

Show Docker disk usage.

docker system prune

Remove unused Docker resources.

Docker Troubleshooting Workflow

# Check running containers
  docker ps
  
  # Check all containers
  docker ps -a
  
  # Check container logs
  docker logs <container>
  
  # Follow logs
  docker logs -f <container>
  
  # Inspect container
  docker inspect <container>
  
  # Check resource usage
  docker stats
  
  # Check Docker disk usage
  docker system df

DevOpsToolbox Tip

When a container is not working, start withdocker ps -a, then check its logs withdocker logs. These two commands often reveal the problem quickly.