How to setup Raspberry Pi 4 Model B with Docker

You're in the right place I'll be showing you how to setup raspberry pi with docker using Raspbian OS or similar

How to setup Raspberry Pi 4 Model B with Docker
Photo by Rubaitul Azad / Unsplash

Installation

Updating your raspberry pi

To start off update your raspberry pi to the latest version using the following command

sudo apt update && sudo apt upgrade

The following command simply updates your raspberry pi to the newest version that way you won't have any issues. The command can take a couple minutes to an hour or so depending on your network speed and how much you have to update

When you run the command the output should look something along the lines of this

Terminal updating the system

Downloading docker

After you've updated your raspberry pi it's time to install docker, once again it's quite simple to do so just run the command

curl -fsSL https://get.docker.com -o get-docker.sh
Note: you might have to run chmod +x ./get-docker.sh to give execute perms

which will get the docker install script for docker if you want to run the test branch of docker use the command

curl -fsSL https://test.docker.com -o test-docker.sh
Note: you might have to run chmod +x ./test-docker.sh to give execute perms

You can verify that you've downloaded the script by running ls and verifying the output shows either get-docker.sh or test-docker.sh

Screenshot of a terminal downloading the Docker script

Installing docker

Once the file is downloaded you want to run it. Execute the script by running the following command

sudo sh ./get-docker.sh

It will then attempt to install docker automatically for you, you can for the most part safely ignore all the stuff that comes up in the terminal as it's just the command it's doing

The output looks something like this

Screenshot of terminal installing Docker

Verifying the install

Once the command is done running you can verify that docker is installed by running

sudo docker info

Which should respond with something like this

Screenshot of a terminal verifying the docker installation

If it returns -bash: docker: command not found restart your raspberry pi and attempt to run the command again. If it still doesn't work run the install script again

Next up we'll be going through how to make docker work without adding sudo first, if you're not interested in that you can skip over that section and move straight to Hello-World


Optional: making docker run as non-root

To make docker run without root you need to create a user group to access the docker container you can do such by running the following command

sudo usermod -aG docker <user>

replace  with your username which by default is pi

For the changes to take effect you have to log out and log back into your account or you can optionally run this command

sudo su - <user>

Once again replace  with your username

For the sake of tutorial, I'll just be running the reboot command which restarts the raspberry pi

sudo reboot

Note: if you decide to do it this way you have to ssh into your raspberry pi again


Hello-World

As with all programming languages or tools nowadays the first thing you do is Hello World so let's get to it

To run the hello world docker container simply write

sudo docker run hello-world

If all is well it should return this

Screenshot of a terminal running the hello-world command

And that's it. Well done! Now you're well on your way to learning docker


Setting up docker-compose (optional)

So the new standard nowadays is setting up docker containers using docker-compose

Since the docker install doesn't come with this you have to install this manually

Installing python3

To run docker-compose you need python3

You can get it by running the following commands

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

Installing docker-compose

After you've installed the dependencies above you can install using pip3 by running this command

sudo pip3 install docker-compose

and that's it now you can use docker-compose in addition to docker! Here's me attempting to setup pi-hole using docker-compose