Virtual servers on a Raspberry Pi with the light weight OS virtualization system, Docker!
I find my self reinstalling the OS on my Raspberry pi all the time because I use my Pi to develop software, and install a lot of software packages, all the time. Sometimes you do experiments where you just can't go back and sometimes you need a clean slate for trying something out. Therefore I was looking for a way to install software, try it out and discard it, in a way that's fast and easy. I also want to isolate my installations sometimes, and sometimes run more than one copy of one piece of software which of course requires some virtual machines.
The solution is Docker!
Docker is a light weight OS virtualization system which makes it possible to run more than one OS on the same hardware and makes it extremely easy to install a new OS, use it and discard it. Another cool feature is that you can download, install, and start an OS with preinstalled software like Owncloud with just one command! Docker also makes your installations portable so you can easily move or clone your installations.
This is what you need:
- A Raspberry Pi, of course!
- A keyboard
- Network connection
- A screen
- and a fast SD-card, mine can transfer 60 M/s which is recommended.
Install Arch Linux on the SD-card
We will now install Arch Linux on your SD-card.
If you are preparing your SD-card on a Win or Mac machine, the instructions for installing Arch Linux on the SD-card can be found here:
1. Download the disk images:
$ wget http://archlinuxarm.org/os/ArchLinuxARM-rpi-latest.zip
2. Unpack ArchLinuxARM-rpi-latest.zip
$ unzip ArchLinuxARM-rpi-latest.zip
3. Find out the path for you SD-card. At my system it's /dev/sdb
Note: You can use Gparted to find the path. We will also use this application later on in this tutorial.
$ sudo dd bs=1M if=ArchLinuxARM-2014.06-rpi.img of=/dev/sdb
Be patient, it will take 10+ minutes and there will be no sign of life while it's going on.
When it's done it will write a few statistics, which also tells you that it's done.
1960837120 byte (2,0 GB) kopieret, 338,551 s, 5,8 MB/s
4. Run this command to ensure that everything have been written to the SD-card:
$ sudo sync
By default, the partition doesn’t fill the entire SD-card which means that you can't use all the space. Therefore we will extend the partitions to make more space for our data.
5. Start Gparted:
$ sudo gparted
Install if necessary...
- Choose the right storage in the top right menu. Mine is /dev/sdb.
- Right click on the partition /dev/sdb2 (extented) and choose modify/move.
- Move the black arrow pointing right over to the right and press modify/move.
- Do the same with /dev/sdb5 (ext4).
- Press the green okay icon on the top to apply all the changes.
- After a little while it all done and you can close the application at take out the SD-card.
Let's get the power on!
Now it's time to start your installation for the first time!
- Pop the SD-card in the the Pi.
- Connect keyboard, network-cable and the HDMI-cable. Note:When everything is connected you connect the power.
1. At this time we don't know the IP-address of the Raspberry Pi so we will log on locally. Log on with the user root and the password root.
Run this command to find the IP of your Pi:
Sometime it's easier to look the IP op through your routers webinterface.
Now when we know the IP we logout by type exit and press [Enter] And use our laptop instead.
2. Start and terminal and connect to the Pi:
$ ssh firstname.lastname@example.org
Password is still root! It's a good idea as you might want to expose your Raspberry Pi to the internet at some point.
3. Change the password by running:
$ passwd root
Write down the password.. Maybe on the back of the Pi ;-) If you're a numpty and forget to do this, then at some point you'll probably forget it. Then you should check out this post on Happy Geekend for changing a password if you've completely forgotten it :)
And then the installation of Docker
1. Download and install Kernel modules and Docker by:
$ curl https://raw.github.com/resin-io/docker-install-script/master/install.sh | sh
2. We want each of the containers (the virtual machines) to have access to the network and internet and therefore we have to enable IP forwardingby running this command:
$ echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.d/40-ip-forward.conf
3. To ensure that Docker will start automatically when the Raspberry Pi is turned on:
$ systemctl enable docker.service
4. The system has to be rebooted before we can use it, as the kernel has been modified and these modifications will only be loaded doing boot.
$ systemctl reboot
It's time for testing!
1. If, for example, you want an Owncloud installation (it's like Dropbox), up and running then connect through ssh as we did before, and run:
docker run -d -i -t -p 443:443 -p 80:80 comzone/rpi-owncloud6
2. When it's up and running, open you webbrowser and go to the IP of your Raspberry pi!
To find many other ready to run installations, search for RPI at: https://index.docker.io/
Only installations which are made for the Raspberry Pi will run!
Everything with docker is managed with the Docker command.
Here are some hints:
- To see all of your containers and their IDs:
$ docker ps
- To attach to a container run:
$ docker attach ID
You have to replace the ID with the id number of the container you cant to manage.
- To delete an container:
$ docker stop ID
$ docker rm ID
TuningYou might want to consider tuning your Raspberry Pi so your CPU runs faster.
Be aware the some SD-cards can become corupted when the Raspberry Pi is tuned. If that happens then you have to reinstall the SD-card again.
It's easy to do!
1. Install an editor, I prefer Nano:
$ pacman -S nano
2. Edit the configurationfile for the Pi:
$ nano /boot/config.txt
3. Uncomment the configuration-group you like. 1Gz is a good speed:
4. Press [CTRL]+x to save and exit.
The change will take place after next reboot so run:
$ sudo systemctl reboot