Diese Webseite nutzt nur technisch notwendige Cookies.

Installing Docker Compose – The Compose File Guide

How can I install Docker Compose?

This guide explains how Docker Compose works.

Let’s get started!

What is Docker?

Docker software virtualisation uses container virtualisation. In these containers, programmes can run isolated from the host system.

Docker als Grundlage
Docker as a basis – Install Docker Compose Steffen Lippke

In contrast to virtual machines, Docker containers are more lightweight and do not consume as many resources. The resources do not have to be fixed at the beginning, but the Docker Engine allocates the resources to the containers that they currently need.

The computer (host) decides which resources and hardware can be allocated to the individual container. Containerisation makes it possible to run multiple instances of the same program on one computer without affecting each other.

Docker containers are based on Docker images (binary blobs), which you start up with a terminal command. The Docker images contain the compiled code of the software and all dependencies. The containers use a configurable and virtualised network and separate storage (volumes).

What is a Docker Compose?

All developers who are too lazy to type into a terminal love a “Docker Compose”. Extended Docker commands are very long and confusing because of the poor readability in the CLI.

Docker Compose ganz einfach - Docker Compose installieren Steffen Lippke

A Docker-Compose file stores the content of the Docker command. A Docker-Compose is not a normal shell script, but a yml / yaml file that provides the necessary parameters.

The developer only has to type docker-compose up to start the container and docker-compose down to shut it down.

Why favour Docker Compose?

These top 3 reasons should persuade you to leave the Docker CLI behind in most cases:

Forgot something again? – Persistent

Docker containers need different parameters depending on the installation. It’s easy to forget a parameter or get lost in the ultra-long command.

The file format saves the command in a readable format.

Manage thousands of snippets – structure

Docker commands can be varied as required.

YAML puts a stop to the chaos and each aspect must be specified individually in a structured manner.

Automatic start and stop

You want to start a Docker container with less than 16 keystrokes:

docker-compose up

// and

docker-compose down

Install

Docker

For Docker Compose, you first need a Docker installation on your computer. If you have a Debian-based system (Ubuntu, Linux Mint, PopOS etc.), simply enter

sudo apt-get install docker.io

You can do this manually. Download the prerequisites:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add the keys / repos:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

apt-cache policy docker-ce

Install Docker via apt:

sudo apt install docker-ce

Start the Docker service:

sudo systemctl status docker

Test the
Command
with:

docker

In Windows, use the Windows Subsystem for Linux (WSL) to get the software running. Alternatively, you can use the desktop programme:

https://docs.docker.com/desktop/install/windows-install/

Download the .exe and run the programme. Follow the installation instructions and complete the installation.

Docker-Compose

In Linux / Debian:

sudo apt-get update 
sudo apt-get install docker-compose-plugin

Docker Compose is part of the Windows version “Docker for Desktop” and does not need to be installed.

The Docker Compose file

Ideally, you should always name the Docker Compose file docker-compose.yml in a repo. This is the default name that Docker always searches for when you type the docker-compose up command.

The file starts with a version number. Each version has a different range of functions. As a rule, you should use the latest version to utilise all available features:

version: "3.8"
 services:
 pihole:
 image: pihole/pihole:latest
 ports:
 - "53:53/tcp"
 - "53:53/udp"
 - "67:67/udp"
 - "50006:80/tcp"
 environment:
 TZ: "Europe/Berlin"
 WEBPASSWORD: "superapssword"
 volumes:
 - "./pihole/etc-pihole/:/etc/pihole/"
 - "./pihole/etc-dnsmasq.d/:/etc/dnsmasq.d/"
 restart: unless-stopped

Under the services tag, you define free names for the Docker containers that you want to run. Start multiple Docker containers in a compose file by specifying multiple images / containers.

Images

Images von Docker Hub - Docker Compose installieren Steffen Lippke
Install images from Docker Hub – Docker Compose Steffen Lippke

The image "pihole/pihole:latest" is freely available from Docker Hub. You can download 100 images a day from there or use your local cache for a new installation.

Ports and network

Then follow the port shares according to the scheme:

Host port (outbound):Docker port (inbound)/transport protocol (tcp/udp)

Instead of specifying the ports manually, the network-mode:hosts mode, like the port shares, works like a natively installed application on a baremetal server. Docker works with different network modes, which you can specify as drivers:

  • host: No isolation between the containers – like a baremetal server
  • bridge: The containers can communicate with each other.
  • overlay: If you want to connect Docker swarms with each other, you need this form of communication.
  • none: Prevents communication in / out.

Environment variables

Depending on the container, you may also need a set of environment variables. You will need to refer to the software documentation.

Volumes

Was wie so speichern
What to store and how – Install Docker Compose Steffen Lippke

The volumes hold all data that must not expire after a restart. Without a Docker volume, the Docker Engine resets the application during startup. You can either create Docker volumes directly in the yml …

volumes:

  myfirstvolume:

… or beforehand with a Docker command:

docker volume create myfirstvolume

Alternatively, you can mount the volumes in a folder on your host file system. The folder that you specify in the Docker-Compose must exist for this:

/etc/pihole/

You can create a folder nesting with mkdir -p /etc/pihole/.

Restart policy

The container adheres to the restart policy if it crashes.

  • always: Restart every time
  • unless-stopped: Like always, the container must only be stopped with the explicit stop command
  • no: No restart
  • on-failure: If an error occurs, the container restarts.

Integration guide with Docker Build

A Docker compose can be easily connected to a Docker build. The Dockerfile contains how a finished image is created from the code, a build and run container. Every Docker-Compose needs an image, which is based on such a Dockerfile. We have previously obtained the images from the Docker Hub. Alternatively, instead of:

image: pihole/pihole:latest

-->


build:.

In our compose file. If the code, the Dockerfile and Docker-compose are in the same folder, the Docker image build starts automatically. The scripts compile the source code, build the image and the compose starts the image with all parameters, volumes and networks. Ingenious!

Leave a Reply

Your email address will not be published. Required fields are marked *


Jeden Monat teile ich mit Mitgliedern
4 neue praxisnahe Tutorials (je 1000+ Wörter).


Trage Deine Mail, damit Du
Deine Coding + Hacking Skills erweitern kannst!