Here are some of the most commonly used Docker and Docker Compose commands.


Basic

- CLI documentation:

docker

- Show help for Docker commands:

docker help

- Show the Docker version information:

docker version
docker --version

- Display system-wide information:

docker info

- Show disk usage:

docker system df

- Remove unused data (containers, images, volumes):

docker system prune --all --volumes

Images

- List all images:

docker images
docker image ls

- Download an image from a registry (e.g., Docker Hub):

docker pull <image>
docker pull ubuntu

docker pull <image>:<tag>
docker pull node:18

- Push image to registry:

docker push myrepo/myapp:v1

- Build an image from a Dockerfile:

docker build -t <name>:<tag> -f <Dockerfile> .
docker build -t myapp:latest -f Dockerfile.dev .

- Tag existing image:

docker tag <source> <target>
docker tag myapp:latest myrepo/myapp:v1.0

- Remove an image:

docker rmi <image>
docker rmi ubuntu

docker image rm <image>
docker image rm node:18

- Remove unused images:

docker image prune
docker image prune -a # remove all

- Remove all images:

docker rmi $(docker images -q)

- View detailed info about an image:

docker inspect <image>
docker inspect ubuntu

- Show image build layers:

docker history <image>
docker history nginx

- Export an image to file:

docker save -o <file>.tar <image>
docker save -o myapp.tar myapp:latest

- Import image from file:

docker load -i <file>.tar
docker load -i myapp.tar

Containers

- List running containers:

docker ps

- List all (including stopped) containers:

docker ps -a

- List all containers id:

docker ps -aq

- Start an existing container:

docker start <container_id>

- Stop a running container:

docker stop <container_id>

- Stop all containers:

docker stop $(docker ps -aq)

- Restart a container:

docker restart <container_id>

- Pause container processes:

docker pause <container_id>

- Resume paused container:

docker unpause <container_id>

- Run a container from image:

docker run <image>

- Run interactively with terminal:

docker run -it <image>
docker run -it ubuntu bash

- Run in background (detached):

docker run -d <image>
docker run -d nginx

- Show port (or specific) mapping for a container:

docker port <container_id>

- Map ports:

docker run -p <host_port>:<container_port> <image>
docker run -p 8080:80 nginx

- Mount volumes:

docker run -v <host_path>:<container_path>
docker run -v /data:/app/data myapp

- Name the container:

docker run --name <name> <image>
docker run --name webserver nginx

- Rename an existing container:

docker rename [container_name] [new_container_name]

- Show detailed info:

docker inspect <container_id>

- View container logs:

docker logs <container_id>

- List the logs from a running container in real-time:

docker logs -f <container_id>

- Show running processes:

docker top <container_id>

- Show live resource usage statistics of all containers (or specific container):

docker stats
docker stats <container_id>

- Execute a command inside a running container (e.g., bash):

docker exec -it <container_id> <cmd>
docker exec -it <container_id> bash

- Attach to running container:

docker attach <container_id>

- Copy from container to host:

docker cp <container_id>:<path> <local_path>
docker cp <container_id>:/var/log ./logs

- Copy from host to container:

docker cp <local_path> <container_id>:<path>
docker cp ./config.json <container_id>:/app/config.json

- Running containers size:

docker container ls -s

- Remove container:

docker rm <container_id>

- Remove all containers:

docker rm $(docker ps -aq)

- Remove all stopped containers:

docker container prune

Networking

- List all networks:

docker network ls

- Create a new network:

docker network create <network_name>

- Inspect network details:

docker network inspect <network_name>

- 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>

- Remove one or more networks:

docker network rm <network_name>

Volumes

- List all volumes:

docker volume ls

- Create a new volume:

docker volume create <volume_name>

- Remove a volume:

docker volume rm <volume_name>

- Inspect a volume:

docker volume inspect <volume_name>

# Docker Compose

- Check Docker Compose version:

docker compose version

- Create and start containers defined in docker-compose.yml:

docker compose up

- Start containers in detached mode:

docker compose up -d
docker compose up nginx -d

- Stop and remove containers, networks, and volumes created by docker compose up:

docker compose down
docker compose down -v --rmi all --remove-orphans

- List containers created by Docker Compose:

docker compose ps

- Start existing containers:

docker compose start

- Stop running containers:

docker compose stop

- Restart all containers:

docker compose restart

- Build images defined in docker-compose.yml:

docker compose build

- Build images without using the cache:

docker compose build --no-cache

- View logs for all services:

docker compose logs
docker compose logs nginx

- Follow the logs (real-time output):

docker compose logs -f

- Run a one-time command in a new container for a service:

docker compose run <service_name> <command>
docker compose run composer create-project laravel/laravel
docker compose run artisan migrate

- List all services in the docker-compose.yml:

docker compose config --services

- List all volumes in the docker-compose.yml:

docker compose config --volumes

- Download images defined in compose file from a registry without starting the containers:

docker compose pull
docker compose pull db

- Upload images for defined services to a container registry:

docker compose push
docker compose push web

Source: Orkhan Alishov's Notes