Skip to the content.

Main Docker cheat sheet



Global

File structure

root
├── /var/lib/docker
│   ├── aufs
│   ├── containers
│   ├── image
│   ├── volume

Access a container

Run command inside a container

docker exec <DOCKER_NAME | DOCKER_ID>

Logs Docker : json format

docker logs <DOCKER_NAME | DOCKER_ID>

Easy Ubuntu bash

docker run -it ubuntu bash

Docker info

Inspect Docker : json format

docker inspect <DOCKER_NAME | DOCKER_ID>

docker inspect <IMAGE_NAME>

Info in docker Host

docker info

Info on image

docker history <IMAGE_ID>

Systeme size

docker system df

Docker compose

desc : Docker compose is use to manage multiple containers

Docker compose up

docker compose up .

Docker build

docker compose build .

example from docker-sample

# version is now using "compose spec"
# v2 and v3 are now combined!
# docker-compose v1.27+ required

services:
  vote:
    build: ./vote
    # use python rather than gunicorn for local dev
    command: python app.py
    depends_on:
      redis:
        condition: service_healthy
    healthcheck: 
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    volumes:
     - ./vote:/app
    ports:
      - "5000:80"
    networks:
      - front-tier
      - back-tier

  result:
    build: ./result
    # use nodemon rather than node for local dev
    entrypoint: nodemon server.js
    depends_on:
      db:
        condition: service_healthy 
    volumes:
      - ./result:/app
    ports:
      - "5001:80"
      - "5858:5858"
    networks:
      - front-tier
      - back-tier

  worker:
    build:
      context: ./worker
    depends_on:
      redis:
        condition: service_healthy 
      db:
        condition: service_healthy 
    networks:
      - back-tier

  redis:
    image: redis:alpine
    volumes:
      - "./healthchecks:/healthchecks"
    healthcheck:
      test: /healthchecks/redis.sh
      interval: "5s"
    networks:
      - back-tier

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"
    volumes:
      - "db-data:/var/lib/postgresql/data"
      - "./healthchecks:/healthchecks"
    healthcheck:
      test: /healthchecks/postgres.sh
      interval: "5s"
    networks:
      - back-tier

volumes:
  db-data:

networks:
  front-tier:
  back-tier:

Docker Run

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Show running Docker

docker ps

# for extra like stopped containers
docker ps -a

Run a docker image by name

docker run <IMAGE_NAME>
# With a specific tag, default tag is `latest`
docker run <IMAGE_NAME>:<TAG>

Running in detach mode of the current bash

docker run -d <IMAGE_NAME>
# Then for reattaching
docker attach <DOCKER_NAME | DOCKER_ID>

Set the name of the container

docker run --name <NAME> <IMAGE_NAME>

Port Mapping

docker run -p <DOCKER_HOST_PORT>:<DOCKER_CONTAINER_PORT> <IMAGE_NAME>

Volume mapping

docker run -v <MAPPED_FOLDER>:<DOCKER_FOLDER> <IMAGE_NAME>
exmaple :
docker run -v /opt/datadir:/var/lib/mysql mysql

# or with mount
docker run \
  --mount type=bind,source:/opt/datadir,target:/var/lib/mysql \
  mysql

# or using volume name
## first create a volume
docker volume create <VOLUME_NAME>
## then map it
docker run -v <VOLUME_NAME>:<DOCKER_FOLDER> <IMAGE_NAME>

[DEPRECATED] Running docker with network links

docker run --name <DOCKER_NAME> --links <OTHER_DOCKER_NAME>:<OTHER_DOCKER_NAME> <IMAGE_NAME>

Running in Interactive Terminal mode

docker run -it <IMAGE_NAME>

Set envirnonment variables

docker run -e <VAR_NAME>=<VALUE>

Kill a container

docker stop <DOCKER_NAME | DOCKER_ID>

Remove a container from the local host

docker rm <DOCKER_NAME | DOCKER_ID>

Image

desc : Images are use to create a specific container

Pull a docker image

docker pull <IMAGE_NAME>

List available images on local host

docker images

Remove a docker image for local host

docker rmi <IMAGE_NAME>

Dockerfile : docs

desc : Dockerfile are use to create images of an application

Dockerfile format

[INSTRUCTION] [ARGGUMENT]

# example
FROM ubuntu                                    # must include an os
RUN apt update && apt -y install python        # download python
RUN pip install django                         # install dependency
COPY . /opt/source-code                        # copy project files from host to image
ENTRYPOINT 

Build Images

Usage : docker build [OPTIONS] PATH | URL | -

# Build an image
docker build -f Dockerfile -t <TAG_NAME> <PATH_TO_DOCKERFILE>
## --file , -f		Name of the Dockerfile (Default is PATH/Dockerfile)
## --tag , -t		Name and optionally a tag in the name:tag format

# example
docker build -f Dockerfile -t drazic/my-app .

# Push an image
docker push <DOCKER_NAME>
# example
docker push drazic/my-app

Share Image

docker save <IMAGE_NAME> -o <FILE_NAME.tar>
docker load -i <FILE_NAME.tar>

Repository Private/Public

Workflow

Create a private repository
ocker run -d --name my-registry -p 5000:5000 --restart alwais registry:2

Tag image to the registry
docker image tag <IMAGE_NAME> <URL>:<PORT>/<IMAGE_NAME>

Push to the repository
docker push <URL>:<PORT>/<IMAGE_NAME>

Pull from the repository
docker pull <URL>:<PORT>/<IMAGE_NAME>

See all available images

curl -X GET <URL>:<PORT>/v2/_catalog
# example
curl -X GET localhost:5000/v2/_catalog