03 March 2015 / Last updated: 30 Mar 2016

Custom Base Images for your Fleet

Execution time:
Difficulty:
Cost:
One of the questions that has been popping up quiet a lot lately is, "Can I build my own base images?".
"Debian isn't my cup of tea, can I build my own base images?"
We here at resin.io love Raspbian/Debian and that is why the default base images are always built from this base. However, if you are looking for something off the beaten track or want to run some more hipster level OS on your devices, then this article is for you.
Its actually pretty easy to roll your own base images. The secret ingredient to getting your custom images to work on resin.io is qemu-arm. All you need to do is add it into your Dockerfile so that our builders are able to do the x86 to ARM cross compilation in the cloud.
In this article we will show you exactly how to do this. After this you should be able to use any of the ARM based images on docker hub as a base for your resin.io projects. We won't go into how to build and push these images on dockerhub. If you are interested in doing this have a look at one of our previous blogs about running docker on ARM.
Note: This article assumes some knowledge of Docker and expects that you have docker or somekind of boot2docker installed on your host machine. It also expects you to have an account over at the docker hub.
Alright lets get into it...

Step 1:

First things, first. We need to find an image we like on dockerhub or build our own. Building our own is a little bit more involved, so we won't get into it, but you can take a look at this if you want to play around with building ARM images. It is important to remember the image we select needs to be architecture specific to our device, i.e. an x86 Ubuntu base image is not going to play nicely with our raspberry pi B+.
For this article, we selected digitallyseamless/archlinux-armv6h, which is built on an Arch Linux distribution targeting armv6. This is the correct architecture for the whole raspberry pi family, except the newest Rasperry Pi 2 B.

Step 2:

From this point, we are going to dive into docker land, so make sure you are familiar with docker commands and terminology. If you are unsure of building docker images, start with a little reading from over here.
We start by cloning this repo and edit its Dockerfile, so that its parent image is the one we selected from dockerhub in step 1.
Here is the dockerfile for the Arch Linux image we selected:
FROM digitallyseamless/archlinux-armv6h


COPY qemu-arm-static /usr/bin/
You will also see we COPY qemu-arm-static into the image. This is needed in the resin.io cloud builders so that all your code can be correctly cross compiled for the targeted ARM architecture.

Step 3:

From the terminal we cd into the folder we just cloned and edited in step 2 and run:
docker build -t shaunmulligan/arch-arm6h-resin .
Where shaunmulligan is my account name on docker hub and arch-arm6h-resin is some fancy name we came up with for our new base image.
Note: The period . at the end of this command is important!!
It takes a short while to pull down the parent image from dockerhub and build our new docker image. Once it is finished we can see it in our docker images list by doing:
docker images
docker images list

Step 4:

Again from the terminal run:
docker push shaunmulligan/arch-armv6h-resin
After a short while we have our new base image ready and waiting on dockerhub. Now we just have to build some thing cool with it!

Step 5:

Next up we just have to make a new resin.io project that uses our shiny new base image. For this we will just make a simple python webserver using the flask framework and as an added bonus just throw in a small ssh daemon so we have local ssh access.
This is what our Dockerfile ends up looking like:
FROM shaunmulligan/arch-armv6h-resin


RUN pacman -Sy --noconfirm python2 python2-flask dropbear




RUN dropbearkey -t dss -f etc/dropbear/dropbear_dss_host_key
RUN dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key




COPY . /app




CMD ["bash", "/app/start.sh"]
Its pretty simple. It bases of our new image shaunmulligan/arch-armv6h-resin, installs some of the libs we need with pacman, then RUN's some key generation stuff for the ssh and then tells the application to start from start.sh in our app directory.
If you want to see the rest of the code, check it out here.
Now if we deploy this we can see our lovely new arch linux based raspberry pi running. See the image below:
docker images list
One pretty cool consequence of this is that other users can share and use my base image, for instance you can now base your resin.io projects on shaunmulligan/arch-arm6h-resin because it is a public image.
So there you have it. Brand new base images for our resin.io projects in 4 simple steps (step 5 doesnt really count :P ). Enjoy!!
Any questions? or you'd just like to say hi, come find us on our community chat.
by Alexandros MarinosFounder/CEO, balena

Share this post