Two of the most powerful tools for modern computing in their own realm – Raspberry Pi and Docker. If you have not heard about them, google search on them would fetch plenty of amazing content on them. In brief, just to set the context for this blog, here it is:
Raspberry Pi – a credit card sized computer with wired, wireless interfaces, powerful in it’s own way, lots of interfacing options through it’s GPIO pins, very good Linux distributions to complement the hardware. Based on the ARM architecture. To learn more visit: Raspberry
Docker – A modern software “tool”, multi platform, a platform of it’s own ( if you can call that?), built on the concept of containers. To learn more visit: Docker
Now, when you bring these two tools together, it is one heck of a combination – a pocket dynamite. It opens up a lot of things.
Here in this blog, I will be demonstrating how to setup the docker environment on the Pi, build a small container from scratch, run the container. The container launches, queries a website for a PHP page which returns the IP address of the node from which the query was made.
Setting Up Docker on Raspberry Pi
Docker now provides a script to install on any platform. The script automatically detects the platform on which it is being installed.
curl -sSL https://get.docker.com | sh
Wait for the installation to complete and make sure it gets installed without any errors or exceptions.
Enable as system service and auto start: sudo systemctl enable docker Starts docker as a service : sudo systemctl start docker Make sure you add docker as user : sudo usermod -aG docker pi
Check if docker is running
Build An Docker Image
Create a directory of your choice in the location of your choice:
mkdir docker_example cd docker_example
Create the Dockerfile with the below contents:
FROM resin/rpi-raspbian:latest ENTRYPOINT  RUN apt-get update && \ apt-get -qy install curl ca-certificates CMD ["curl", "http://pradeepclicks.com/myData/test.php"]
FROM – Docker standard statement telling docker to pick up the base system to start with and build on it. Here is it resin/rpi-raspbian:latest
ENTRYPOINT  – to take control of what the entry point is going be and override the default for the Resin image.
RUN – Docker standard to run commands.
Here the system is being prepared with tools to query a PHP page from command line. curl and ca-certificates, SSL certificates are being installed on raspbian.
CMD – Start up command for this container, docker run invokes the lines followed by CMD. Here it is querying a PHP page on a website and printing it’s content on screen.
Build the Docker Image
sudo docker build -t my_docker
You will see an output like the below: │Sending build context to Docker daemon 2.048kB ~ │Step 1/4 : FROM resin/rpi-raspbian:latest ~ │ ---> e60721112344 ~ │Step 2/4 : ENTRYPOINT  ~ │ ---> Running in 488417056adf ~ │Removing intermediate container 488417056adf ~ │ ---> 31b00564ef03 ~ │Step 3/4 : RUN apt-get update && apt-get -qy install curl ca-certificates ~ │ ---> Running in 334ef363e4f3 . . . . │Fetched 13.7 MB in 1min 52s (122 kB/s) ~ │Reading package lists... ~ │Reading package lists... ~ │Building dependency tree... ~ │ca-certificates is already the newest version. ~ │curl is already the newest version. ~ │0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. ~ │Removing intermediate container 334ef363e4f3 ~ │ ---> bce998306b8c ~ │Step 4/4 : CMD ["curl", "http://pradeepclicks.com/myData/test.php"] ~ │ ---> Running in b064a0f8b845 ~ │Removing intermediate container b064a0f8b845 ~ │ ---> 97c25308ed2b ~ │Successfully built 97c25308ed2b ~ │Successfully tagged my_docker:latest
Run the Docker image
sudo docker run my_docker
~ │You have visited from....[ 18.104.22.168 ]
This completes the exercise. To summarize, we
1. Setup the docker environment of Raspberry Pi.
2. Configured docker on Raspberry Pi.
3. Created a Dockerfile with contents to query a webpage using curl, installing curl in the process.
4. Build the docker container.
5. Run the docker container.
You could try more by:
1. Creating docker hub account.
2. Experimenting with official containers.
3. Exploring more docker cli options.
Please leave your comments about the post and/or if you need any more information.