Docker Cheatsheet

Some frequent docker commands parameters

Page content

Here is a Docker cheat sheet covering the most important commands and concepts from installing to running containers and cleaning up:

docker logo

Installation

On Ubuntu

sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
sudo apt install docker-ce
sudo systemctl start docker

Docker Compose Installation

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
``").

## General Docker Commands
### Version and System Info
```bash
docker version          # Displays Docker CLI and daemon versions
docker system info      # Lists data about your Docker environment
docker help             # View the help index
docker <command> --help # View help information for a specific command

Login and Logout

docker login            # Login to Docker Hub
docker logout           # Logout from Docker Hub

Image Management

List Images

docker images           # List all images
docker images -a        # List all images, including intermediate images

Pull Images

docker pull <image-name:version> # Download an image from Docker Hub

Build Images

docker build -t <image-name> . # Build an image from the current directory's Dockerfile
docker build -f <Dockerfile-path> -t <image-name> . # Build an image from a specific Dockerfile
docker build --build-arg foo=bar . # Build an image with build arguments
docker build --pull . # Pull updated versions of images referenced in FROM instructions
docker build --quiet . # Build an image without emitting output

Tag and Push Images

docker tag <local-image-name> <username>/<preferred-image-name>
docker push <username>/<preferred-image-name>

Remove Images

docker rmi <image-name>        # Remove a specific image
docker image prune             # Remove unused images
docker image prune -a          # Remove all unused images

Container Management

Run Containers

docker run -itd --name <container-name> <image-name> # Run a container in detached mode
docker run -it -p <host-port>:<docker-port> <image-name> # Run a container with port mapping
docker run -it --name <container-name> <image-name> # Run a container interactively

List Containers

docker ps                  # List running containers
docker ps -a               # List all containers
docker ps -s               # List running containers with CPU and memory usage

Start, Stop, and Restart Containers

docker start <container-name>   # Start a stopped container
docker stop <container-name>    # Stop a running container
docker restart <container-name> # Restart a container

Remove Containers

docker rm <container-name>      # Remove a stopped container
docker rm -f <container-name>   # Forcefully remove a container
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) # Remove all containers

Attach to Containers

docker attach <container-name>  # Attach to a running container
docker exec -it <container-name> bash # Run a command in a running container interactively

Inspect Containers

docker inspect <container-name> # Inspect a container
docker logs <container-name>    # View a container's logs
docker port <container-name>    # Show a container's port mappings

Network Management

docker network ls            # List all networks
docker network create <network-name> # Create a new network
docker network rm <network-name>    # Remove a network

Volume Management

docker volume ls             # List all volumes
docker volume create <volume-name> # Create a new volume
docker volume rm <volume-name>    # Remove a volume
docker run -v <host-path>:<container-path> <image-name> # Mount a volume

Docker Compose

Basic Commands

docker-compose up           # Start services defined in docker-compose.yml
docker-compose up -d        # Start services in detached mode
docker-compose down         # Stop and remove services
docker-compose ps           # List running services
docker-compose logs         # View logs of services
docker-compose start        # Start services
docker-compose stop         # Stop services
docker-compose pause        # Pause services
docker-compose unpause      # Unpause services

Dockerfile Commands

Build Image from Dockerfile

docker build -t <image-name> <Dockerfile-path> # Build an image from a Dockerfile

Example Dockerfile

FROM <base-image>
RUN <command>
COPY <source> <destination>
EXPOSE <port>
CMD ["<command>", "<argument>"]

Security and Secrets

Docker Secrets

docker secret create <secret-name> <file> # Create a secret
docker secret ls                         # List all secrets
docker secret rm <secret-name>          # Remove a secret

Docker Security

  • Use Docker secrets to centrally manage sensitive data and securely transmit it to containers.
  • Secrets are encrypted during transit and at rest in a Docker swarm.

Cleanup

Remove Unused Resources

docker system prune          # Remove unused data (images, containers, networks, volumes)
docker image prune           # Remove unused images
docker container prune       # Remove unused containers
docker network prune         # Remove unused networks
docker volume prune          # Remove unused volumes

Remove untagged images

Sometimes after big good compile there is a picture like this fomr the docker images command:

lot of untagged images

to remove those images with tags:

docker rmi $(docker images --filter “dangling=true” -q --no-trunc)

this above is old and incompatible. dangling=true parameter is obsolete. Use

docker system prune

it will ask:

WARNING! This will remove:
	- all stopped containers
	- all volumes not used by at least one container
	- all networks not used by at least one container
	- all dangling images

You might decide to say Yes to this warning…

This cheat sheet covers the essential commands and concepts needed to manage Docker containers, images, networks, volumes, and more. For a deeper dive, refer to the detailed guides and tutorials available online.