Setup your Raspberry Pi for Docker and Docker-Compose

Setup your Raspberry Pi for Docker and Docker-Compose

Personally, I like to use Docker containers on my Raspberry Pis as they come with a great layer of abstraction and portability. Here is how to get your Raspberry Pi ready for Docker and Docker-Compose.

1. Setup your Raspberry Pi with Raspbian, Wifi and SSH access

If your Raspberry Pi is already set up, you can skip this step and continue to step 2.

The most straight forward way to install Raspbian on your Raspberry Pi is through the official Raspberry Pi Imager. It comes with all the tools needed to prepare and flash an SD card with the Raspbian version you want to install. For this tutorial, I am assuming, that you are using the smallest Raspbian "Lite" version.

To ensure, that SSH is enabled on the first start of your Raspberry Pi, we need to add an additional empty file called ssh to the SD card's root directory after is has been flashed with the image.

touch ssh

This is especially useful, if you are running your Raspberry Pi in headless mode with no screen or keyboard attached.

If you want to connect the Raspberry Pi to a Wifi network, it might be useful to also provide the network details for the first startup. For this, define a wpa_supplicant.conf file in the same root folder with the following content. Make sure, it uses the line feed (LF) newline character.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert 2 letter ISO 3166-1 country code here>

network={
 ssid="<Name of your wireless LAN>"
 psk="<Password for your wireless LAN>"
}

If you are unsure about your 2 letter ISO 3166-1 country codes, you can look it up here.

After booting up your device, you can connect to it via SSH by using its IP address, that you will likely get from your router's web interface.

ssh pi@111.222.333.444

The default username is pi and the standard password is raspberry.

It's highly recommended to change the password on the first connection. You can do this and configure many other useful things in the handy Raspberry Pi Config tool.

 sudo raspi-config

2. Install Docker

The connection is set up, it's time to install Docker. Fortunately, Docker provides a handy install script for that.

curl -sSL https://get.docker.com | sh

After the script has finished, add the permissions to the current user to run Docker commands.

sudo usermod -aG docker ${USER}

Reboot the Raspberry Pi to let the changes take effect.

3. Install Docker-Compose

Docker-Compose usually gets installed using pip3. For that, we need to have python3 and pip3 installed. If you don't have it installed, you can run the following commands.

sudo apt-get install libffi-dev libssl-dev
sudo apt install python3-dev
sudo apt-get install -y python3 python3-pip

Once python3 and pip3 are installed, we can install Docker-Compose using the following command.

sudo pip3 install docker-compose

4. Enable the Docker system service to start your containers on boot

This is a very nice and important addition. With the following command you can configure your Raspberry Pi to automatically run the Docker system service, whenever it boots up.

sudo systemctl enable docker

With this in place, containers with a restart policy set to always or unless-stopped will be re-started automatically after a reboot.

5. A sample Docker Compose file

This section shows a quick sample of a Docker-Compose file, which starts three containers that once started will automatically come up, if the Raspberry Pi get fully power cycled. To learn more about the sample project I used here, visit my Docker Speed Test project on GitHub.

version: '3'
services:
  # Tests the current internet connection speed
  # once per hour and writes the results into an
  # InfluxDB instance
  speedtest:  	
    image: robinmanuelthiel/speedtest:0.1.1
    restart: always
    depends_on:
      - influxdb
    environment:
      - LOOP=true
      - LOOP_DELAY=3600 # Once per hour
      - DB_SAVE=true
      - DB_HOST=http://influxdb:8086
      - DB_NAME=speedtest
      - DB_USERNAME=admin
      - DB_PASSWORD=<MY_PASSWORD>
      
  # Creates an InfluxDB instance to store the
  # speed test results
  influxdb:
    image: influxdb
    restart: always
    volumes:
      - influxdb:/var/lib/influxdb
    ports:
      - "8083:8083"
      - "8086:8086"
    environment:
      - INFLUXDB_ADMIN_USER=admin
      - INFLUXDB_ADMIN_PASSWORD=<MY_PASSWORD>
      - INFLUXDB_DB=speedtest
      
  # Displays the results in a Grafana dashborad
  grafana:
    image: grafana/grafana:latest
    restart: always
    depends_on:
      - influxdb
    ports:
      - 3000:3000
    volumes:
      - grafana:/var/lib/grafana
      
volumes:
  grafana:
  influxdb:
docker-compose.yaml

To start the containers using Docker-Compose, run the following command.

docker-compose -f docker-compose.yaml up -d

☝️ Advertisement Block: I will buy myself a pizza every time I make enough money with these ads to do so. So please feed a hungry developer and consider disabling your Ad Blocker.