Docker has, for a lot of cases, become the go to way to install and run certain services. Docker is a container system which means it comes with all the advantages of containers such as ease of installation, resource management and security. Docker is also fairly low overhead so it’s possible to run a number of containers on a small device like a Raspberry Pi. This article will cover installing Docker on your Raspberry Pi and running a simple test container.
This guide assumes you have an installed and working Raspberry Pi 4. I’m working on an 8GB model, you’ll probably be able to run a few containers on the 4GB model but for this you really need as much memory as you can get. I haven’t installed the desktop environment to save resources so all the configuration will happen via the command line.
Update Your Pi
Start the install process by updating your Pi as this will help to avoid weird issues further down the line. Docker is complex software with a lot of moving parts so the more problems we can avoid the better. The reboot step may or may not be required depending on what gets updated, it can’t hurt though.
sudo apt update sudo apt upgrade sudo reboot now
In the past installing Docker on the Raspberry Pi was quite a bit of work but for a while now it’s been a semi-supported platform. Unlike some other platforms you can’t simply install Docker from the supplied apt repository you need to use a script provided by Docker. Run the following command to get the installation script:
curl -fsSL https://get.docker.com -o get-docker.sh
Obviously you need to be careful about downloading and running scripts from the Internet, only run this if you trust Docker. Curl is a powerful command line application for downloading data and is installed on nearly every Linux machine. The flags, -fsSL, mean fail, silent, show error and location. The -o flag tells Curl where to output the script. This command will cause the file “get-docker.sh” to appear in the current directory.
The script needs to be run with root privileges and will configure the package manager automatically – updates will happen via apt as per other applications. Run the script with the following command:
sudo sh get-docker.sh
Now you probably want to add yourself to the Docker group so that you can run “docker” without sudo. This is a potential security risk but it’s probably an acceptable risk if your setup is limited to your own personal network.
sudo usermod -aG docker $USER
To apply the above change you need to execute either run “netgrp docker” or better log out and back in (or reboot). Once you’re back at a terminal check the docker version. As can be seen I’m running 20.10.21.
docker version Client: Docker Engine - Community Version: 20.10.21 API version: 1.41 Go version: go1.18.7 Git commit: baeda1f Built: Tue Oct 25 18:01:19 2022 OS/Arch: linux/arm64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.21 API version: 1.41 (minimum version 1.12) Go version: go1.18.7 Git commit: 3056208 Built: Tue Oct 25 17:59:41 2022 OS/Arch: linux/arm64 Experimental: false containerd: Version: 1.6.9 GitCommit: 1c90a442489720eec95342e1789ee8a5e1b9536f runc: Version: 1.1.4 GitCommit: v1.1.4-0-g5fd4c4d docker-init: Version: 0.19.0 GitCommit: de40ad0
Testing the Docker Install
Docker has access to many containers straight out of the box, one of those is the Hello World container. When you run “hello-world” Docker pulls the latest image and then executes it. All the image does is output a message indicating that Docker is installed correctly.
docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 7050e35b49f5: Pull complete Digest: sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (arm64v8) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
You might now want to install Docker Compose or Portainer, both of which help you to work with containers.