Headless way to setup Raspberry-Pi and Docker

(λx.x)eranga
Effectz.AI
Published in
4 min readOct 25, 2019

--

I got raspberry-pi 4 with canakit. In this post I’m gonna discuss about setting up raspbian OS, configuring SSH, wifi setup and configuring docker on raspberry-pi. The information about assembling raspberry-pi 4 canakit can be found in this video.

Setup Raspberry-Pi

1. Identify SD card

Connect SD card to computer and find the disk(SD card) name of the SD card with diskutil. This command will return available disk list. You can find the SD card name at the bottom of the list. In my case /dev/disk2 is the name of the SD card.

2. Format SD card

Format SD card with ExFat format. Then unmount SD card if SD card was automatically mounted.

3. Burn raspbian image to SD card

Download raspbian image and mount it to SD card. I can do it via dd command. It will add raspbian image into boot partition in the SD card.

4. Enable SSH

SSH is not enabled by default on raspberry-pi. To enable SSH, I need to place an empty file named ssh (no extension) in to the boot partition of the SD card.

5. Add wifi network info

I’m gonna connect raspberry-pi into wifi network. To do that I need to add another file wpa_supplicant.conf into the boot partition of the SD card. This file need to popup with wifi network informations. When booting the pi this file will be copied to the main partition’s /etc/wpa_supplicant location and replace whatever is there.

I have created wifi hotspot on mac-book with internet sharing and connected the raspberry-pi into that hotspot. Read more about internet sharing on mac-os in here. I have added my wifi hotspot name/password in to ssid and psk fields. When Raspberry-Pi boot-up, this wpa_supplicant.conf file will be copied to /etc/wpa_supplicant/wpa_supplicant.conf inside the Pi. Then I can to SSH into the Pi and edit this file to add more network entires. Read more information about troubleshooting wifi with wpa_supplicant from here.

6. Boot raspberry-pi from SD card

Now everything set to boot the raspberry-pi from SD card. eject SD card from the computer and inset into raspberry-pi. Then Plug a USB-C power supply cable into the power port. Wait till few seconds until booting up the raspberry-pi.

7. Find IP address of raspberry-pi

To SSH into raspberry-pi I need to find the IP address of the raspberry-pi. In latest raspberry-pi’s comes with default host name raspberry-pi.local. I can directly connect to this host via SSH. In old raspberry-pi version I need to find the IP address via arp or nmap commands.

arp returns devices found on the local network, including PCs, routers, iPhones, and iPads, displaying both their LAN IP address as well as their individual MAC address.

nmap(network mapper) command can be use to detect what hosts are available on the network, what services (application name and version) those hosts are offering, what operating systems (and OS versions) they are running, what type of packet filters/firewalls are in use, and dozens of other characteristics. When creating hotspot on mac, it generates new subnet. I can find the subnet and scan the hosts available on that subnet via nmap.

8. SSH into raspberry-pi

After finding the ip addresses I can connect to raspberry-pi via SSH. The default username is the pi, password is the raspberry.

Setup docker

1. Install docker

After connecting to raspberry-pi I can install the docker packages via apt package manager.

2. Install docker-compose

Next step is to install docker-compose. I can do that via pip python package manager.

3. Run docker

The CPU architecture of raspberry-pi is ARM(not x86/x64 by Intel or AMD). The docker images build on x86/x64 architecture will not work on raspberry-pi. If I run x86/x64 based docker image on raspberry-pi it will causes an error. To run docker image on raspberry-pi I need to build it with compatible to ARM CPU architecture. To do that I can build the images based on arm32v6/alpine or resin/rpi-raspbian base image. Following is an example mongodb docker image that I have built based on resin/rpi-raspbian:jessie base image.

This docker image can be built from any computer(ex linux, mac-os, rabberry-pi), push to docker hub and run on raspberry-pi. In my scenario I have built the image from mac-os.

Backup SD card

Once raspberry-pi is setup, I can take a backup of the SD card with all the installed packages and configurations. It’s good idea to take backup time to time. If raspberry-pi get corrupt I can restore the backup image without redoing whole setup from the beginning. The backup image can also be use to setup new raspberry-pi as well. Following is the way to take a backup image from SD card and restore.

Reference

  1. https://desertbot.io/blog/headless-raspberry-pi-4-ssh-wifi-setup
  2. https://medium.com/@viveks3th/how-to-bootstrap-a-headless-raspberry-pi-with-a-mac-6eba3be20b26
  3. https://medium.com/a-path-to-pi/adding-ssh-and-wifi-to-a-headless-raspberry-pi-3-fresh-install-54be6634716e
  4. https://withblue.ink/2019/07/13/yes-you-can-run-docker-on-raspbian.html
  5. https://www.docker.com/blog/happy-pi-day-docker-raspberry-pi/
  6. https://chrisschuld.com/2019/09/installing-docker-and-docker-compose-on-raspberry-pi4-with-raspian/
  7. http://osxdaily.com/2016/11/03/view-lan-device-ip-address-arp/
  8. https://www.youtube.com/watch?v=Ct9XwyYvmbU&t=487s
  9. https://dzone.com/articles/building-arm-docker-images-on-the-raspberry-pi
  10. https://blog.alexellis.io/getting-started-with-docker-on-raspberry-pi/
  11. https://thepihut.com/blogs/raspberry-pi-tutorials/17789160-backing-up-and-restoring-your-raspberry-pis-sd-card
  12. https://raspberrypi.stackexchange.com/questions/11631/how-to-setup-multiple-wifi-networks

--

--