In this article we are going to learn how to install and run Docker on the Raspberry Pi. We are going to use a simple Docker Image as our example that will deliver a JSP page with Tomcat. By the end of the article, we are going to build up a swarm with three Raspberry Pi.
With Docker you can install applications with all their dependencies in images. The images are then easily exchangeable between systems. There are already a lot of preconfigured images that you can use freely and adapt to your own needs. Using them saves you a lot of time and resources. When an image is started, a container is created. Images and containers behave similarly to each other, like Java classes and their instances. In theory, any number of containers can be created from an image. The only limitation is the hardware on which the containers run. However, this limit can easily be moved up if you use a Docker Swarm. With a swarm you can connect several Docker installations running on different computers. They can then be managed like one installation. A Docker Swarm offers a certain tolerance against errors;
The images are not changed during a container’s runtime. The containers hold their own data in an overlay file system. Each newly started container initially has the same data as the image from which it was created. However, there is also the possibility to restart a container with its data. We will go into this in more detail later. Additionally, Docker provides so-called volumes. With these it is possible to use parts of the host file system in a container and thus to store data persistently, independent of the status of the container. There are two Docker variants:
We are going to use Docker CE in this article.
For our swarm we use three Raspberry Pi of type 3, on which we install the current Raspbian Buster Lite. We use the Lite variant, because it does not contain any unnecessary programs. You can download Raspbian Buster Lite for free from the download page of the Raspberry Project [1]. Once you have saved the ZIP file on your disk, all you have to do is to unzip it and write it to the SD cards with a tool of your choice. We need one card for each Raspberry. It will take a while for all of the cards to be written. IT people who are familiar with Murphy’s law (“Everything that can go wrong will go wrong.”) Will prepare one more card right away, to be on the safe side.
You should label each Raspberry uniquely so that you don’t get confused later, and it helps to give the Raspberries static IP addresses and unique hostnames. Table 1 shows an example of how the assignment can be carried out. Of course, you have to choose IPs that work in your own network. Figure 1 shows the labeled Raspberry swarm. You should use a fan to provide some airflow, because the Raspberries can get quite warm when they are so closely together.
Fig. 1: The Raspberry Swarm
Hostname | IP | Label |
docker01 | 192.168.3.70 | 01 |
docker02 | 192.168.3.71 | 02 |
docker03 | 192.168.3.72 | 03 |
Table 1: Configuration example of the swarm
When you have everything ready, it is time to put the SD cards into the Raspberries. We describe here what to do for the setup, using the computer docker01 as an example. For the other two computers the same steps have to be carried out analogously.
At the initial boot, the Raspberry adjusts the size of the root file system to the used SD card, then it reboots automatically. After the reboot we can log in onto the Raspberry as usual (User: pi Password: raspberry ). First of all, you should activate the SSH server on the Raspberry, so that you can access the Raspberry from your PC. You can do this by using the command line tool raspi-config . The tool must be run with superuser rights ( sudo raspi-config ). Now you can activate the SSH server in the menu item Interfacing Options | P2 SSH.
From now on you can continue working from your PC. Linux users can simply use the command ssh pi @ <RASPI_IP> for this. If you are using a different operating system, you will need to download a tool, Putty for instance, in order to establish the SSH connection.
Now we give each Raspberry a static IP address, which makes it easier to distinguish them from one-another. To accomplish this, we edit the file, in which the network settings are stored ( sudo nano /etc/dhcpcd.conf ). In Listing 1 you can see the location in the file that needs to be changed in order to use a static IP.
Listing 1: Using a static IP
#Example static IP configuration:
interface eth0
static ip_address = 192.168.3.70 / 24
static routers = 192.168.3.1
static domain_name_servers = 192.168.3.1
The hostnames of the Raspberries are currently all the same. Therefore, we should change them as well. The file / etc / hostname contains the hostname of a Unix machine. Please open the file with root privileges and change the name ( sudo nano / etc / hostname ). Now there is a second file which should be adapted: / etc / hosts . Here, you should also replace the name raspberry with the corresponding name: docker0x . The file / etc / hosts will be discussed again in the next section. In order for our changes to take effect, you should reboot the raspberry ( sudo reboot ).
Now we can access the three Raspberries using their IP addresses. It would be nice, if we could use the host names instead of the IP. An easy way to do so, is to enter the host names with the IP addresses into the hosts file. With the hosts file it is possible to use a local name resolution. On Linux systems the file can be found under / etc / . On Windows it is located in c: \ windows \ system32 \ drivers \ etc _ . You will need a superuser permission to edit it. Now you can simply add the hostnames and IPs to the file, as shown in Listing 2. Once the file is saved, you can use the names to access the Raspberries. However, the names only work on the computer to which the hosts file is adapted. Now you can also adapt the_hosts files on the Raspberries, so that they...