Installing Docker on the Pi 5
The quick and easy way to install docker to allow you to run software on your Pi.
Why use Docker?
Let's get straight to the point, it's a neat and tidy way to install applications on your systems without messing up root file system. By this I'm referring to the days gone by when you used to use one server for one job, e.g. a web server, you'd install apache and php onto the system and run your websites through that. All's well and fine unless you want to install a different version of PHP in order to support say a legacy application. At this point it can become very messy trying to maintain multiple versions as your package manager might try and block this due to several reasons such as common files clashing. By putting your application, binaries and config into a Docker container you can isolate these from each other and run multiple versions without harming the other applications, this just makes things neat and if there's one thing I like on my systems it's a neat file system not cluttered with miscellaneous files and work arounds.
Another good reason to use containers is that in that example I mentioned earlier you end up wasting a lot of CPU and RAM resources that sit there idling. I remember the days when systems would sit in the single digital figures of CPU usage and it was such a waste. Docker however lets you run lots of apps at the same time. Now you do have to consider that the CPU and RAM usage will spike if a particular container gets busy but with careful planning you can minimise the impact on other systems, but generally speaking you'll get a denser (much better) utilisation of your system when using containers.
The good news is it's pretty simple to install docker on your Pi and the steps below should get you going pretty quickly. We'll even install "docker compose" which is a tool for running a set of closely related containers as a complete package. A good example of this would be WordPress which you would want to run the application server but also a database server for the complete stack of software.
Install Docker and Compose
- First lets make sure theres no unoffical packages in place that will conflict by removing them.
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
- Now lets set up dockers official apt repository:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
- Now install the tool chain:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin vim
- Verify that the installation is successful by running the hello-world image:
sudo docker run hello-world
👉 Note
If you want to run docker as a non root user you'll need to add that user to the docker group by running the following command:sudo usermod -aG docker $USER
Setup storage directories on the host
Now some applications can live entirely in the container and it doesn't matter then when it's closed down the data goes away, however, in other situations you may want to store persistent data and you can do this in a few ways but for easy we'll map the directories to the local disk. I like to try and keep the persistent data neat and tidy on my system (are you noticing a theme here) so I make sure to group my configs and data directories together. For this I store them in/opt/containers/<application_name>
- Let's start by creating that folder structure, for ease we are going to use sudo to become root for these commands(you could just prefix all instructions with sudo if you really wanted):
sudo mkdir -p /opt/containers
What next?
Well there's lot you can do next you can start trying out docker containers and compose files to run some of the applications you want to and to make it even easier to run compose files and I'll be publishing this article very soon so be sure to subscribe!