Create a Docker Container for Raspberry Pi to Blink an LED

In this post, we are going to see how to create a custom Docker Image using Dockerfile, that can interact with the GPIO Pins and performs some operation on the Raspberry Pi. So let’s begin and create our first Container for Raspberry pi –

Step 1. Install Docker on Raspberry Pi:

First You need a Raspberry Pi with Raspbian OS. If Your is not ready, You canDownload Raspbian from Raspberrypi.org ‘s download section: https://www.raspberrypi.org/downloads/raspbian/. You need to flash this downloaded image to the micro SD card. Assuming your laptop has a SD card slot, you need a flashing software like etcher. Go ahead and download from: https://etcher.io/

Recommended: How To Use Raspberry pi in a truely headless mode

Now Your Pi Ready, Open terminal and follow given below Steps.

Update Packages:

Update Raspberry Pi packages using the following command –

sudo apt-get update && sudo apt-get upgrade

Install Docker:

Install Docker using following command –

curl -sSL https://get.docker.com | sh

Add permission to User to run Docker Commands:

Add “pi” user to “docker” group using the following command –

sudo usermod -aG docker pi

You must Log off from Raspberry Pi and Login again, for this to take effect.

Verify Installation:

Check Docker installation using the “docker –version” command. If you see the correct version, you are good to go.

[email protected]:~ $ docker --version
Docker version 17.05.0-ce, build 89658be
[email protected]:~ $

Step 2. Select the Base Image for Dockerfile:

Now You need to create a Dockerfile for the new Docker Image. Docker can build images automatically by reading the instructions from a Dockerfile. You can find most of the Docker Base Images on the official Docker Hub. You need to be careful while selecting the base image for your new Docker Image, as most these bases images are created for specific CPU Architectures. You can find the list of different CPU Architectures and their respective Docker Hub URLs from the following link –

https://github.com/docker-library/official-images#architectures-other-than-amd64

You can use the “cat /proc/cpuinfo | grep model” command on your Raspberry Pi to find the CPU Architecture and use the corresponding Docker Hub repository –

[email protected]:/ $ cat /proc/cpuinfo | grep model
model name : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
[email protected]:/ $

Now, if you are using Rasbian, you may have “jessie” or “wheezy” variants. You can run the “cat /etc/os-release” command to see the Linux details on your Raspberry Pi –

[email protected]:~/docker_test $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
[email protected]:~/docker_test $

I am going to use “arm32v7/python” base image with tag “2.7.13-jessie” which is available at the following link (you can also use the default image with tag “2.7” that will make the image name as “arm32v7/python:2.7“) –

https://hub.docker.com/r/arm32v7/python/

Step 3. Create a New Directory on Raspberry Pi:

Create a new directory for all the files that you will use for creating a new Docker Image and get into this new directory. Let’s name it “docker_test” –

mkdir docker_test
cd docker_test

Step 4. Create a Python Script to Blink an LED:

Now We Create a Python Script to blink the LED with the name “led_blinker.py“, using the following code and save it in the “docker_test” directory created in the previous step –

import RPi.GPIO as GPIO
import time

# Configure the PIN # 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT)
GPIO.setwarnings(False)

# Blink Interval 
blink_interval = .5 #Time interval in Seconds

# Blinker Loop
while True:
 GPIO.output(8, True)
 time.sleep(blink_interval)
 GPIO.output(8, False)
 time.sleep(blink_interval)

# Release Resources
GPIO.cleanup()

Step 5. Create Dockerfile:

Now we Create a new file in the “docker_test” directory with the name “Dockerfile“, using the following contents –

# Python Base Image from https://hub.docker.com/r/arm32v7/python/
FROM arm32v7/python:2.7.13-jessie

# Copy the Python Script to blink LED
COPY led_blinker.py ./

# Intall the rpi.gpio python module
RUN pip install --no-cache-dir rpi.gpio

# Trigger Python script
CMD ["python", "./led_blinker.py"]

If you need more information about Dockerfile, Visit This –

https://docs.docker.com/engine/reference/builder/

Step 6. Create Docker Image from Dockerfile:

Now we Create Docker Image with the image name as “docker_blinker” and tag as “v1” using the following command –

docker build -t "docker_blinker:v1" .

Now you should be able to list the image on your Raspberry Pi using the following command –

docker image ls

Step 7. Prepare the Circuit:

Connect an LED via a 330 Ohm register (or any value between 200 – 300 Ohm) to Raspberry Pi GPIO PinsRPI Pinout

Step 8. Start the Container to Blink the LED:

We need to use “docker container run” command with either the “--privileged” option or by specifying the Linux GPIO Device (“/dev/gpiomem“) using the “–device” option.

You can use one of the following commands to run the Docker Container

docker container run --device /dev/gpiomem -d docker_blinker:v1

or 

docker container run --privileged -d docker_blinker:v1

I hope you like this post “Create a Docker Container for Raspberry Pi to Blink an LED”. Do you have any questions? Leave a comment down below! Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Explore Some more Raspberry Pi Tutorials :


Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm

3 thoughts on “Create a Docker Container for Raspberry Pi to Blink an LED

Leave a Reply

%d bloggers like this: