DEV Community

Cover image for How To Install Docker and Docker-Compose On Raspberry Pi
Alemaño
Alemaño

Posted on • Updated on • Originally published at dotruby.com

How To Install Docker and Docker-Compose On Raspberry Pi

Prerequisites

  • Raspberry Pi with a running Raspbian OS
  • SSH connection enabled

To do this you can check Raspberry Pi Setup.

1. Update and Upgrade

First of all make sure that the system runs the latest version of the software.
Run the command:

sudo apt-get update && sudo apt-get upgrade
Enter fullscreen mode Exit fullscreen mode

2. Install Docker

Now is time to install Docker! Fortunately, Docker provides a handy install script for that, just run:

curl -fsSL test.docker.com -o get-docker.sh && sh get-docker.sh
Enter fullscreen mode Exit fullscreen mode

3. Add a Non-Root User to the Docker Group

By default, only users who have administrative privileges (root users) can run containers. If you are not logged in as the root, one option is to use the sudo prefix.
However, you could also add your non-root user to the Docker group which will allow it to execute docker commands.

The syntax for adding users to the Docker group is:

sudo usermod -aG docker [user_name]
Enter fullscreen mode Exit fullscreen mode

To add the permissions to the current user run:

sudo usermod -aG docker ${USER}
Enter fullscreen mode Exit fullscreen mode

Check it running:

groups ${USER}
Enter fullscreen mode Exit fullscreen mode

Reboot the Raspberry Pi to let the changes take effect.

4. 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
Enter fullscreen mode Exit fullscreen mode

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

sudo pip3 install docker-compose
Enter fullscreen mode Exit fullscreen mode

5. 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
Enter fullscreen mode Exit fullscreen mode

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

6. Run Hello World Container

The best way to test whether Docker has been set up correctly is to run the Hello World container.
To do so, type in the following command:

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Once it goes through all the steps, the output should inform you that your installation appears to be working correctly.

7. 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, visit 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:
Enter fullscreen mode Exit fullscreen mode

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

docker-compose -f docker-compose.yaml up -d
Enter fullscreen mode Exit fullscreen mode

Find Raspberry Pi Docker Images

Raspberry Pi is based on ARM architecture. Hence, not all Docker images will work on your Raspberry Pi.

Remember that when searching for images to pull from Docker Hub. Apply the Architectures filter to search for supported apps.

How to Upgrade Docker on Raspberry Pi?

Upgrade Docker using the package manager with the command:

sudo apt-get upgrade
Enter fullscreen mode Exit fullscreen mode

How to Uninstall Docker on Raspberry Pi?

You can simply remove docker using the package manager:

sudo apt-get purge docker-ce
Enter fullscreen mode Exit fullscreen mode

Note: Depending on the version of software, you may need to use an additional command to completely remove Docker:

sudo apt-get purge docker-ce-cli
Enter fullscreen mode Exit fullscreen mode

To delete leftover images, containers, volumes and other related data, run the following command:

sudo rm -rf /var/lib/docker
Enter fullscreen mode Exit fullscreen mode

Edited configuration files must be deleted manually.

References

Top comments (9)

Collapse
 
nickramsbottom profile image
Nick Ramsbottom

For anyone who is struggling to get docker compose to install you can get it using apt:

sudo apt install docker-compose

Collapse
 
therealdakotal profile image
Dakota Lewallen • Edited

Was flopping around in the docker docs and they never mentioned compose was available through pip. Glad I found your article!

Collapse
 
kevinyou profile image
Kevin You

I tortured myself by trying to install docker-compose using pip instead of just using apt.

Because pip now requires rust, it involves:

It may be easier to just use apt like Nick suggests.

Collapse
 
oldsurferdude profile image
OldSurferDude

Installing Docker on RPi per this page. I get to the command:

curl -sSL get.docker.com | sh

and it fails at

  • sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null E: Sub-process /usr/bin/dpkg returned an error code (1)

Should I ignore it? Should I continue?

OSD

Collapse
 
vj_andrei profile image
Andreas Koutsoukos
Collapse
 
elalemanyo profile image
Alemaño

thanks @vj_andrei. @oldsurferdude I updated docker installation command. Let me know if it works.

Collapse
 
aniran profile image
aniran

Latest release of influxdb apparently dropped support for arm/v7 architecture.
Please specify image influxdb:1.8.10 , otherwise you'll get "no matching manifest for linux/arm/v7 in the manifest list entries"

Collapse
 
readaboutstuff profile image
Read About Stuff

This is sweet! I actually just made a similar guide.

readaboutstuff.net/2021/08/01/inst...

Collapse
 
greenfrogsb profile image
Mariusz Krezel

Great article, if anyone would like to automate installation process and some container deployment, please check out my method:
greenfrognest.com/lmdsondocker.php