Cheatsheet: Docker

Basic Commands

Check Docker version

docker --version

Run a container

docker run -d -p 80:80 nginx

List running containers

docker ps

Stop a running container

docker stop <container_id>

Remove a container

docker rm <container_id>

Images

List Docker images

docker images

Pull an image from Docker Hub

docker pull <image_name>

Build an image from a Dockerfile

docker build -t <image_name> .

Remove an image

docker rmi <image_name>

Volumes

Create a volume

docker volume create <volume_name>

List volumes

docker volume ls

Remove a volume

docker volume rm <volume_name>

Use a volume in a container

docker run -d -v <volume_name>:/path/in/container <image_name>

Networks

Create a network

docker network create <network_name>

List networks

docker network ls

Connect a container to a network

docker network connect <network_name> <container_id>

Disconnect a container from a network

docker network disconnect <network_name> <container_id>

Docker Compose

Start services defined in docker-compose.yml

docker compose up -d

Stop services

docker compose down

View running services

docker compose ps

Rebuild services

docker compose up -d --build

Cookbook

Debug a container

docker exec -it <container_id> /bin/bash

View logs of a container

docker logs <container_id>

Copy files from a container

docker cp <container_id>:/path/in/container /host/destination

Limit container resources (CPU and Memory)

docker run -d --cpus="1.5" --memory="500m" <image_name>