---
title: Docker
url: 'https://www.niklas-stephan.de/en/wiki/software/docker'
markdown: 'https://www.niklas-stephan.de/en/wiki/software/docker.md'
lang: en
date: '2021-12-28'
description: 'Docker Docker helps us, through so-called containers, to set up the technical server infrastructure that is needed for the most various projects quickly and isolated from the base system. Besides that we reduce the maintenance effort through the use of Docker, e.g. for system updates and backups. T…'
---

#  Docker 

Docker helps us, through so-called containers, to set up the technical server infrastructure that is needed for the most various projects quickly and isolated from the base system. Besides that we reduce the maintenance effort through the use of Docker, e.g. for system updates and backups. Thousands of projects are available pre-fabricated, which can merely be started with a simple command. In this article I describe how Docker and the additionally helpful Docker Compose are installed. Docker Compose helps us to orchestrate whole container landscapes. An example: in the installation of WordPress via Docker several containers are needed, one for the app and one for the database. If we now start all these individually and perhaps want to pass certain parameters along, it quickly becomes unclear. With Compose we can summarize all needed parameters and containers in one structured file and control this environment with simple commands.

## Prerequisites

We assume that we use a Linux system based on Debian as operating system. That can be e.g. the Raspberry Pi OS, Ubuntu or one of the many other variants thereof. Basically Docker however also works under all other Linux systems and also under Windows or macOS.

## Installation of the Docker Engine

We always want to use the newest versions and therefore orientate ourselves on the instructions on the official Docker pages for that:

1. Install Docker: <https://docs.docker.com/engine/install/debian/>
2. Steps for a more comfortable use of Docker: <https://docs.docker.com/engine/install/linux-postinstall/>

If one is not overwhelmed by the apparently large mass of steps, we see that in the end we only had to copy 3–4 commands per copy & paste into our command line.

Best we carry out a reboot of our system once more at the end.

## Use of Docker Compose

The structure of a Docker Compose configuration is in the end always the same and based on a single file, the `compose.yml`. Best one does not simply put this somewhere, but thinks about a folder structure, since one perhaps wants to operate many environments in parallel and keep the overview. An example:

- we create in the directory `/opt/` a subdirectory `/opt/docker/`.
- And here again subdirectories, divided e.g. by projects or URLs. So for example `/opt/docker/wordpress/`.
- Into this subdirectory then belongs our file for the definition of the environment, i.e. `/opt/docker/wordpress/compose.yml`.
- If we stay at the example WordPress, we get on [https://hub.docker.com/\_/wordpress](https://hub.docker.com/_/wordpress) an example for the content of the `compose.yml`.

We want to adapt this example somewhat more for ourselves and optimize it for permanent operation. That can then look like this (note: the formatting with the free characters as tabs is binding):

```yaml

services:
  wordpress-app:
    container_name: wordpress-app
    hostname: wordpress-app
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: wordpress-db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
      TZ: Europe/Berlin
    volumes:
      - ./wordpress:/var/www/html
    logging:
      options:
        max-size: "10m"
        max-file: "3"

  wordpress-db:
    container_name: wordpress-db
    hostname: wordpress-db
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'      
      TZ: Europe/Berlin
    volumes:
      - ./db:/var/lib/mysql
    logging:
      options:
        max-size: "10m"
        max-file: "3"
```

For the explanation:

- `service`, `container_name`, `hostname`: Naming only the service itself is obligatory. We set the other two variables in order to be able to find our container quickly depending on the kind of access, when we are searching for it sometime.
- `image`: The application which we want to use, by default an image from Docker Hub <https://hub.docker.com/search?q=>.
- `restart`: `always` means that the container is restarted, no matter for whatever reason it previously aborted/crashed.
- `environment`: Various environment variables that are passed on to the container. These can be quite different depending on the image.
- `volumes`: Persistent data that we e.g. also take into account for a backup, resp. fundamentally have under our control and do not want to have \\"hidden\\" in the container. Through `./` we determine that it concerns a subdirectory of the current folder. If not present it would be created automatically, better however to create it oneself beforehand (permissions etc.).
- `logging`: Some containers/applications are quite eager in writing log files. Hereby we restrict this to three 10 MB large files, whereby the oldest entry is overwritten upon exceeding the limit.
- `ports`: If no reverse proxy is used, e.g. `8080:80` indicates that the port 80 running in the container is forwarded to the host system under port 8080. I.e. if in our example we want to call up WordPress, then with: <http://HOST:8080>. For the communication of the containers with each other, e.g. from app to DB, no port release is needed! That simultaneously increases the security of our environment.

## Useful commands

From the directory in which we have created our `compose.yml` we can now start our environment with `docker compose up -d`. If we want to shut down all containers from the Compose file, that works with `docker compose down`. With the command `docker compose pull` we pull the newest version of our images from Docker Hub. After the pull a renewed `docker compose up -d` is required in order to start the containers in the new version. The persistent data from the defined volumes naturally remains preserved. It becomes dangerous only at larger version jumps within the application. Then there is either the possibility to shut the environment down, delete the last loaded image and then start again. Alternatively in the `compose.yml` a version of the image can also be set fixed. Therewith however one in general also forgoes security updates.

Why for heaven's sake should one assign fixed IP addresses to containers? An absolutely legitimate question to which the answer is: best never! Because: Docker comes with a kind of built-in DNS and DHCP server and manages the IP address wonderfully by itself. Unfortunately there are nevertheless special cases in which one can simplify life if one sets IPs manually. Such a special case would e.g. be given if the application that is operated in a container cannot deal with hostnames but only with IP addresses in order to access a further container.

Formerly that was a relatively complicated undertaking, but with the current Docker (Compose) versions that is child's play too.

## Finding out the IP addresses of running containers

If you already have various containers running within a network, it would be dangerous to assign a fixed IP address only to some of them. Why? Because it could then happen to you e.g. after a reboot of your server that the automatically assigned IP of one container intersects with one fixedly assigned by you of another container. The result would be that the second container cannot start, since the IP is already given out.

To find out which IP address is currently assigned to a container, the following command helps us:

```bash

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINERNAMEoderID
```

If one wants to find out the IP addresses of all running containers, this command is very useful:

```bash

docker inspect $(docker ps -q ) --format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'
```

## Setting up a network

In our example we want to assign fixed IP addresses to all containers that lie in a network created for our Caddy reverse proxy. If you have no such network yet, you can create it via the command:

```bash

docker network create --subnet=172.28.0.0/16 proxy
```

After this has occurred, we can now use the IP addresses `172.20.0.X` in the Docker Compose definitions of the individual containers, whereby X stands for the numbers 1–255.

## IP assignment with Docker Compose

We begin with the assignment of a fixed IP for our Caddy reverse proxy:

```yaml

services:
  caddy:
    container_name: caddy
    image: caddy:latest
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - ./caddy_data:/data
      - ./caddy_config:/config
    logging:
        options:
          max-size: "10m"
          max-file: "3"
    networks:
        proxy:
          ipv4_address: 172.28.0.1

volumes:
  caddy_data:
  caddy_config:
networks:
    proxy:
        external: true
```

After saving and the subsequent restart of the environment, the IP is fixedly assigned. Decisive are the blocks:

```yaml
    networks:
        proxy:
          ipv4_address: 172.28.0.1
```

as well as:

```yaml

networks:
    proxy:
        external: true
```

Now we proceed the same way with all other containers with a respectively individual IP and have successfully finished our undertakings.

## Conclusion

Even if the assignment of fixed IP addresses is relatively simple, the whole appears as somewhat \\"unclean\\". Thus the procedure does contradict the principle that I take the definition of a Docker container and can start it at any time on another server/host. That is no longer possible like this, because initially a new network has to be created manually. On the other hand the manual creation of the network is required anyway if one defines a kind of DMZ (own network) for one's reverse proxy. Only the manually given subnet range is the additional effort here. Thus the use of fixed IP addresses remains a special case which only makes sense in certain scenarios.

---

## Navigation

- Parent: [Software](https://www.niklas-stephan.de/en/wiki/software.md)
- Previous: [🪣 Distrobox](https://www.niklas-stephan.de/en/wiki/software/distrobox.md)
- Next: [Filesystems](https://www.niklas-stephan.de/en/wiki/software/filesystems.md)
