Docker

docker pull

pull image
# pull hello-world image
> docker pull hello-world

docker run

docker run
# run hello world container
> docker run hello-world

# run hello world and remove container when done
> docker run --rm hello-world

# run container and open interactive shell
> docker run --rm busybox ping -c 4 1.1.1.1

# run container and open interactive shell
> docker run --rm -it busybox sh

# run container in daemon mode
> docker run --rm -d busybox sleep 100

# name container as "my-nginx"
> docker run --rm -d --name my-nginx nginx

docker exec

docker exec
# execute a command on a already running container
> docker exec my-nginx ls /etc/nginx

# use a shell in interactive-mode
> docker exec -it my-nginx sh

docker volume

volumes
# create a volume
> docker volume create foo-volume

# list volumes
> docker volume ls

# inspect volume
> docker volume inspect foo-volume

# remove volume
> docker volume rm foo-volume

# attach a volume to a container
> docker run -v backup-volume:/var/opt/backup \
  -d --name my-nginx nginx

# mount folder from host to container
> docker run -v /srv/html:/usr/share/nginx/html \
  -d --name my-nginx nginx

# mount file from host to container
> docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf \
  -d --name my-nginx nginx

docker network

volumes
# create network
> docker network create my-network

# create network and specify CIDR
> docker network create my-network --subnet 10.0.0.0/16

# list networks
> docker network ls

# inspect specific networks
> docker network inspect my-network

# remove network
> docker network rm my-network

# run container in specific network with static address
> docker run -d --name my-nginx \
  --net=my-network --ip 10.0.0.11 nginx

# connect a running container to network
> docker network connect my-network container1

# same as above but with resolvable hostname
> docker network connect --alias web my-network container1

docker expose

expose ports
# publish container port 80 to port 8080 on host (host:container)
> docker run -d -p 8080:80 nginx

# publish port 8080 on host to specific interface (interface:host:container)
> docker run -d -p 10.10.0.100:8080:80 nginx

# specify protocol
> docker run --rm -p 1234:1234/udp busybox nc -ulp 1234

docker logs

docker exec
# show all container logs
> docker logs my-nginx

# follow live container logs
> docker logs -f my-nginx

docker build

Dockerfile
# base image
FROM debian:12

# run cmd
RUN apt update && \
    apt install -y \
        wget \
        rsync \
        htop
build image
# build image from Dockerfile
> docker build -t my-image .
run container
# run container as single-shot command
> docker run --rm my-image rsync --help

# run container interactivly
> docker run -it --rm my-image htop

docker push

run container
# docker login
> docker login registry.example.com

# build image using repo URL as build tag
> docker build -t registry.example.com/username/repo:version .

# push container to registry
> docker push registry.example.com/username/repo:version

docker-compose

minimal docker-compose.yml
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: nginx
    container_name: custom-nginx
      networks:
        my-network:
          aliases:
            - web
      environment:
        - "FOO=bar"
      ports:
        - "10.10.0.100:8080:80"
      volumes:
        - $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro
        - /srv/html:/usr/share/nginx/html
        - backup-volume:/var/opt/backup
networks:
  my-network:
    external:
      name: my-network
volumes:
  backup-volume: