cheatsheet-docker

Docker CheatSheet

Docker is a platform that allows you to create, deploy, and run applications inside containers, providing an isolated and consistent environment.

Docker Installation

Install Docker on Windows

Download Docker Desktop from the official site.

Install Docker on Linux

For Ubuntu, you can use the following commands:

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Copied!

Check Docker version

Verify if Docker is installed and display the current version.

docker --version
Copied!

Docker Images

List local images

Displays all images downloaded on the local system.

docker images
Copied!

Download an image

Downloads an image from Docker Hub.

docker pull image_name
Copied!

Remove an image

Removes a specific image from the local system.

docker rmi image_name
Copied!

Search for an image

Searches for an image on Docker Hub from the terminal.

docker search image_name
Copied!

Build an image from a Dockerfile

Creates a new image from a Dockerfile.

docker build -t image_name:tag .
Copied!

View the history of an image

Displays the layers of an image and how it was built.

docker history image_name
Copied!

Tag an image

Adds a tag to an image for easier reference.

docker tag image_id image_name:tag
Copied!

Containers

Create and run a container

Runs a container from a specified image.

docker run image_name
Copied!

Run a container in the background (detached mode)

Runs a container in the background, useful for long-running applications.

docker run -d image_name
Copied!

Assign a name to a container

Specifies a custom name for the container at creation time.

docker run --name my_container image_name
Copied!

View running containers

Lists all containers that are currently running.

docker ps
Copied!

View all containers

Lists all containers, both running and stopped.

docker ps -a
Copied!

Stop a container

Stops the execution of a container.

docker stop container_id
Copied!

Start a stopped container

Starts a previously stopped container.

docker start container_id
Copied!

Remove a container

Removes a container that has been stopped.

docker rm container_id
Copied!

Remove all stopped containers

Removes all containers that have been stopped.

docker rm $(docker ps -a -q)
Copied!

Interacting with Containers

Access a running container

Opens an interactive terminal inside a running container.

docker exec -it container_id /bin/bash
Copied!

Restart a container

docker restart container_id
Copied!

Show logs of a container

Displays the output logs of a container.

docker logs container_id
Copied!

Copy files to/from a container

Copies files from the host system to the container or vice versa.

docker cp host_path container_id:/container_path
Copied!

Stop all running containers

Stops all active containers.

docker stop $(docker ps -q)
Copied!

Dockerfile

A Dockerfile is a text file that contains all the instructions to build a Docker image.

Create a Dockerfile

A Dockerfile defines the runtime environment of an application inside a container.

  • FROM: Defines the base image.
  • RUN: Executes commands during the image build.
  • COPY: Copies files from the host to the container.
  • WORKDIR: Sets the working directory.
  • CMD: Specifies the command to be executed when the container starts.
# Use a base image
FROM ubuntu:20.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip

# Copy files to the container
COPY . /app

# Set the working directory
WORKDIR /app

# Default command
CMD ["python3", "app.py"]
Copied!

Build an image from a Dockerfile

Creates an image from a Dockerfile.

docker build -t image_name .
Copied!

Ignore files with .dockerignore

Defines files that should not be copied into the Docker image.

node_modules
.git
.env
Copied!

Docker Networking

Create a new network

Creates a custom network.

docker network create network_name
Copied!

List Docker networks

Displays all networks created in Docker.

docker network ls
Copied!

Run a container on a specific network

docker run -d --name container-name --network network-name image-name
Copied!

Connect a container to a network

Connects a container to a specific network.

docker network connect network_name container_id
Copied!

Disconnect a container from a network

Disconnects a container from a network.

docker network disconnect network_name container_id
Copied!

Remove a network

Removes a previously created network.

docker network rm network_name
Copied!

Docker Volumes

Create a volume

Creates a persistent volume that can be shared between containers.

docker volume create volume_name
Copied!

Mount a volume in a container

Mounts a volume in a container to store persistent data.

docker run -d --name my_container -v volume_name:/path_inside_container image_name
Copied!

List existing volumes

Displays all volumes created on the system.

docker volume ls
Copied!

Remove a volume

Removes a Docker volume.

docker volume rm volume_name
Copied!

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications.

Basic example of docker-compose.yml

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
Copied!

Start services defined in docker-compose.yml

Starts the services defined in the docker-compose.yml file.

docker-compose up
Copied!

Start services in the background (detached mode)

Starts the services in the background.

docker-compose up -d
Copied!

Stop all services

Stops the execution of all defined services.

docker-compose down
Copied!

View service logs

Displays the logs of all running services.

docker-compose logs
Copied!

Scale services

Increases the number of instances of a service.

docker-compose up --scale service=number
Copied!

Optimization and Cleanup

Clean resources

Remove stopped containers, unused images, and unused volumes

Automatically cleans up unused resources.

docker system prune
Copied!

Remove all unused images

Removes all images that are not used by any container.

docker image prune -a
Copied!

Remove unused volumes

Removes all volumes that are not in use.

docker volume prune
Copied!

Monitoring

View resource usage of a container

Displays the CPU, memory, and other resource usage of a container in real-time.

docker stats container_id
Copied!

View Docker system details

Provides information about the Docker state, such as resources and system usage.

docker info
Copied!

Docker Swarm

Docker Swarm is a tool for creating and managing Docker clusters.

Initialize a Swarm

docker swarm init
Copied!

Create a Service

docker service create --name my-service image-name
Copied!

Scale a Service

docker service scale my-service=5
Copied!

Leave a Swarm

docker swarm leave --force
Copied!