Install Docker and Docker Compose on Raspberry Pi 4 through CLI

Objective

Install Docker, and supporting software, on a headless RPi 4 operating a beta version of Raspberry PI 0S 64-bit Beta version.

Introduction

We are going to install Docker and Docker Compose on Raspberry Pi 4 running the beta 64 bit RPi OS configured to run headless-ly to let us easily manager containers .

Docker is a way to install and operate numerous services like WordPress, Pi Hole, NextCloud as modules (called “images”) on a RPi.  The advantages is that (i) less effort to install the image than it would be to download, configure and maintain the same software independently and (ii) a problem with one service is not likely to affect other services.  There is supposedly little additional drain on the RPi, however you have to learn how to use it.  There is also a security risk because any person can create images with intentional or accidental vulnerabilities, so it is recommended to only use official images.

Requirements

  • Raspberry Pi 4
  • 64 Bit Raspberry Pi OS installed, with headless configuration (see here)
  • 3-Amp, 5 volt Power Supply.

Recipe

  • Update and upgrade.

sudo apt update

sudo apt upgrade

  • Download the script.

curl -fsSL https://get.docker.com -o get-docker.sh

  • Run the Script.

sh get-docker.sh

  • Grant Privileges to use Docker to Pi

sudo usermod -aG docker $USER

  • Reboot.

sudo reboot

  • Confirm Docker Installation.  When you log back in through SSH, you can run the Hello World for docker.  This line will download the official Hello World image, create a container, and then run it:

docker run hello-world

  • List Docker Containers.  Now you can check to see all the containers

docker container ls -a

You should get a list of  with the container ID, name and showing status of exited.

  • Remove the Container.

docker rm [container id or container name]

If you list the active containers again, you’ll see the container has been removed.

  • Confirm PIP.  The next step will make require that you have the ability to install python modules on your RPi.  Type pip3 list and confirm that you get a list of installed Python modules.  If PIP is not installed, you can install it following the directions here.
  • Install Docker Compose.

sudo pip3 install docker-compose

  • When I first rant this command, I ran into errors “Failed building wheel for cffi.”  I ran the following to install the correct packages:
  1. First, I install a package that CFFI depends on, at least in Raspian OS:  sudo apt install libffi-dev
  2. I also followed the recommendation to install CFFI because “wheels are built for cffi”:  sudo apt install cffi
  3. Finally, I re-tried to install DC:  sudo pip3 install docker-compose
  4. After quite a slow install process, it seemed to install.
  • Confirm Installation.  Because of the hiccup, I wanted to confirm correctly installed, so I ran the following:

docker-compose version

You should see confirmation that you are running docker-compose version 1.27.4 or later.  Congratulations.

References

Links

Docker Commands

  • Start a new container:  docker container run [image]
  • List active containers: docker container ls
  • List all containers: docker container ls -a
  • Delete container:  docker container rm [container ID]

Docker-Compose Commands

  • See current version:  docker-compose version
  • Start the yml (in current directory) and run in detached/background mode:  docker-compose up -d
  • See what containers are running:  docker-compose ps
  • Stop containers:  docker-compose stop
  • Remove containers and volumes:  docker-compose down –volumes