Docker

Docker uses the kernel of the host system and the host operating system, applications and their dependencies run in a container isolated from the rest. The container image contains the application to be run plus all necessary libraries.

Single docker applications are run with: docker run <application> it checks locally if it finds a container with the application, if not it tries to download it from the Internet https://hub.docker.com.

docker ps -a what is locally available

docker ps shows what docker is running.

For gentoo https://wiki.gentoo.org/wiki/Docker emerge app-containers/docker and docker-cli for the docker command line command.

Important

for gentoo read the elogs to see if kernel options are missing, or cat /var/log/portage/elog/app-containers\:docker-28.0.4\:20251010-123137.log | grep CONFIG or

run /usr/share/docker/contrib/check-config.sh to see if the kernel contains everything docker requires

for systemd systemctl enable docker.service or for OpenRC rc-update add docker default

usermod -aG docker <username>

docker run hello-world

docker inspect hello-world

docker rm -f <container> delete a container

docker logs -f <container> check the logs

docker --help

The docker applications are stored once under /var/lib/docker.

The -v option behaves as a link between system and the docker container.

As example -v /opt/docker/homeassistant:/config creates a link between the systems /opt/docker/homeassistant directory and the dockers config

docker compose

Often it is necessary to start multiple docker containers together. docker compose does this.

A project directory as /opt/<name> has to be created and a the file docker-compose.yml needs to be added. A good example is more advanced Home Assistant configuration that requires go2rtc stream server, mosquitto MQTT broker and esphome:

services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - ./ha-config:/config
      - /etc/localtime:/etc/localtime:ro
      - /run/dbus:/run/dbus:ro
    restart: unless-stopped
    network_mode: host
    privileged: true
    cap_add:
      - NET_ADMIN
      - NET_RAW

  go2rtc:
    container_name: go2rtc
    image: alexxit/go2rtc
    volumes:
      - ./go2rtc-config:/config
    restart: unless-stopped
    network_mode: host

  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto
    ports:
      - "1883:1883"
    volumes:
      - ./mosquitto-config/config:/mosquitto/config
      - ./mosquitto-config/data:/mosquitto/data
      - ./mosquitto-config/log:/mosquitto/log
    restart: unless-stopped
    network_mode: host

  esphome:
    container_name: esphome
    image: ghcr.io/esphome/esphome:latest
    volumes:
      - ./esphome-config:/config
      - /etc/localtime:/etc/localtime:ro
    environment:
      - TZ=Europe/Zurich
    restart: unless-stopped
    network_mode: host

The characters ./ in docker-compose.yml mean the directory where docker-compose.yml is.

Before starting the first time some containers required a configuration. As mosquitto ./mosquitto-config/config/mosquitto.conf

listener 1883
allow_anonymous true

In side this directory it is then called as docker compose up -d also this pulls (downloads) all from https://hub.docker.com if not locally found. It also creates the directories containing persistent date defined by volumes:

Note

The docker systemd service is aware of the running docker containers and since restart: unless-stopped is used no additional systemd service is required

run docker compose down && docker compose up -d when the file got modified

A quick test if mosquitto works when docker compose is up: In one console docker exec -it mosquitto mosquitto_sub -t test and then in an other docker exec -it mosquitto mosquitto_pub -t test -m hello

Maintenance with docker-compose

cd to the docker-compose.yml directory then docker compose ps or docker compose images to see what is there and when it was created.

docker compose exec mosquitto mosquitto -h | head -n 3 will run the program with the help option so the version should pop up

docker compose pull will pull (download) if available the newest versions but will not execute them. A container has an image and the image has layers, pull shows how many layers will be downloaded and how many are downloaded..

docker compose pull <container> does the same but just for one container

Now docker compose ps will show sha256 hash for the new versions

docker compose up -d <container> recreates the container and in case it finds a new image it is uses this and therefore updates it. -d is for detach and run in background.

docker compose logs --tail=50 <container> to see if it is happy

docker images --filter dangling=true show images not used, unfortunately it does not tell what they are

docker image prune removes the not used images

docker images shows all the images also the ones outside of docker-compose.yml. It has an U in Use flag showing that they are running.

docker and network_mode

Network ports might cause conflicts between docker, multiple docker containers and the host system.

Therefore different options exist:

When set

network_mode: host

the docker container connects directly to the host. This works easy but has a potential conflict when the system already uses this port number. Withing the docker container the hsst address will be 127.0.0.1

When network_mode: host is not set then the docker container will work in bridge mode and requires some network setup.

docker utilities

partainer is a gui for docker and docker compose

An alternative to docker is podman


Linurs startpage