Docker on Raspberry Pi 3

Posted by Tobiasz Kedzierski on 06.08.2019

Docker on Raspberry Pi 3

What and why?

Docker is a tool similar to virtual machine but without its performance overhead. It allows to create totally separated conteneraized environments which use a host machine kernel. So it is as fast as when you run it natively while creating containers or using already built images is a quite easy and pleasant process. For more information about Docker, check sources at the bottom of the page.

Docker is great, so why not use it on Raspberry (RPi)? Usually when I play with RPi I break something in the system or SD card stops working, so I have to reinstall the system and go again through the installation steps. It is not difficult and there are plenty of tutorials which show how do it. I've found that almost everyone of them is different and most don't work from start to end as they are supossed to. So to make my life slightly easier I prepared a short description how to set-up RPi and a short python script which installs Docker and docker-compose while I go to the kitchen and prepare hot cup of tea. Moreover, this script does not have to be downloaded, just copy-paste one line to the terminal and your earl gray is ready to be brewed.

Setting up Raspbian

Download Raspbian Buster Lite and create a bootable SD-card. For this purpose you can use for e.g. Startup Disk Creator (Ubuntu) or Etcher (Windows, Mac, Linux).

If you use a headless RPi it is possible to enable SSH and Wi-Fi before the first boot.\ To enable SSH you have to put an empty file named ssh onto the boot partition without any extension.

For Wi-Fi you need to define wpa_supplicant.conf (configuration file for Wi-Fi Protected Access client and IEEE 802.1X supplicant) on the boot partition as well. This file shall contain credentials for your network.

# wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=«your_ISO-3166-1_two-letter_country_code»

network={
    ssid="«your_SSID»"
    psk="«your_PSK»"
    key_mgmt=WPA-PSK
}

Docker installation

Installation is provided by a simple script which executes just 6 shell commands. I will describe each one of them briefly.

The meaning of particular commands:

  • sudo apt-get update downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies.
    • sudo - executes command as the superuser.
    • apt-get - the command-line tool for handling packages.
    • update - option used to re-synchronize the package index files from their sources.
  • curl -sSL https://get.docker.com | sh installs docker by executing Docker installation script from an online source.
    • curl - tool to transfer data from or to a server.
    • --s/--silent - silent or quiet mode. Doesn't show progress meter or error messages.
    • -S/--show-error - when used with -s it makes curl show an error message if it fails.
    • -L/--location - if the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
    • | - pipes '|' send the output of one command as input of another command.
    • sh - Bourne shell.
  • sudo usermod -aG docker $USER`\ adds your user to the docker group which is created during the installation to run docker commands without without prepending sudo (as non-root user)
    • usermod - modified a user account.
    • --a, --append - adds the user to the supplementary group(s). Use only with the -G option.
    • -G, --groups - a list of supplementary groups.
    • docker - group name.
    • $USER - environmental variable with username.
  • sudo systemctl enable docker\ configures Docker to start on boot
    • systemctl - may be used to introspect and control the state of the "systemd" system and service manager.
    • enable NAME..., enable PATH... - Enables one or more units or unit instances.
    • docker - deamon process name.
  • sudo apt-get install -y python3-pip installs package installer for Python 3
    • install - this option is followed by one or more packages desired for installation.
    • -y, --yes, --assume-yes automatic yes to prompts. Assume "yes" as answer to all prompts and run non-interactively.
    • python3-pip - Python3 package installer.
  • sudo pip3 install docker-compose installs docker-compose through Python package installer
    • pip3 - a recursive acronym for "Pip Installs Packages"
    • install - argument for pip3, self-explanatory.
    • docker-compose - name of package to be installed.

To run this script you just have to copy-paste the following line in the RPi terminal:

curl -sSL https://gist.githubusercontent.com/TobKed/75337ec7b73a0ac59a415b837927e4ee/raw/docker_on_raspbian.py | python3

It executes (similar to curl -sSL https://get.docker.com | sh) this script posted on github gist.

After script successfully finishes you can run subshell as a docker user by executing newgrp docker (newgrp - change the current group ID (GID) during a login session) or restart RPi by sudo shutdown -r now (recommended).



Sources: