This article explains how to install Docker on a Raspberry Pi, how to build a simple image, and how to run a container based on it.
Docker provides a mobile execution environment in which to run software. It is a very popular container technology. Docker is useful in the rapid deployment of applications and in application isolation. It is a valuable tool for developers, users and systems administrators. In this procedure, it will be used to install a simple video processing application called “comskip“.
Although the article is entitled Install Docker on Raspberry Pi, this procedure will work unaltered on most Linux distros, which is one of the advantages of Docker.
Besides acting as a demonstration for those wanting to learn about Docker, this article is aimed at readers of my previous articles How to Watch and Record Live TV with a Raspberry Pi and How to Install Comskip on a Rasperry Pi (without Docker).
Use the following command to install Docker.
$ sudo apt-get install docker.io
wait a few minutes for the install to complete.
Create a Docker File
A Docker image will be created from the definition below. First, create a sub directory:
$ cd $ mkdir -p docker/comskip $ cd docker/comskip
Now, using vi, nano or another editor, create a new file. Name the file “Dockerfile” and make its contents as below:
##Comskip image. ##Pull the ubuntu base image FROM ubuntu ## The maintainer name and email LABEL maintainer="Pi user" ## List all packages that we want to install ENV PACKAGES git build-essential libargtable2-dev \ libavformat-dev libsdl1.2-dev libsdl2-dev ffmpeg autoconf \ libtool sudo cifs-utils vim RUN apt-get update && apt-get install -y $PACKAGES RUN git clone git://github.com/erikkaashoek/Comskip RUN cd Comskip && ./autogen.sh && ./configure && make install # RUN sudo mkdir /Recordings # RUN echo //<osmc IP address>/Recordings /Recordings \ # cifs uid=nobody,gid=nogroup,password=mybackups,acl,noauto \ # 0 0 >> /etc/fstab
Save the file.
Build the Image
Build the image as follows:
$ sudo docker build --rm=true -t comskipimg .
This command will perform many tasks. The standard base image “ubuntu” is pulled (downloaded) from Docker Hub. Several libraries and tools are installed into it. Next, the comskip source code is obtained (cloned) from Github. Lastly, comskip is compiled. The whole process took about 16 minutes on a Pi2, so be patient.
Note the last four lines of the Docker file are commented out and don’t do anything for the moment. More on that later.
Run the Docker Container
We have freshly built Docker image, containing all of the bits and pieces needed to run comskip. Although many items of software have been installed, eg. ffmpeg and git, these have not been installed onto your Pi. Instead, they have been installed into the Docker image. All that has been installed on the Pi is Docker itself.
In order to bring that image to life, run a container. Run a container based on the image as follows.
$ sudo docker run -i -t --privileged --cap-add SYS_ADMIN --cap-add DAC_READ_SEARCH comskipimg root@747fbdb287ca:/#
The container is running and you are left with a prompt. This is a container shell and commands issued here are executed in the container, not directly on the host (your Pi).
The command we want to run is comskip. For demonstration, I have uploaded a video file into the container.
root@747fbdb287ca:/# comskip Minder.2020-04-06.09-15.ts ... 72586 frames decoded in 667.17 seconds (108.96 fps) Commercials were found.
Success! The video file, a 50 minute episode of Minder, was processed in 667 seconds, about 11 minutes.
Access to Video Files
Comskip can be used to process TV recordings created by apps like OSMC and Kodi. If you have a media center PC or similar, its recordings folder could be mounted into the Docker container. If you have shared out the recordings directory, eg. by configuring Samba on an OSMC server, then the last 4 lines of the Docker file can be used to mount the share.
To try it, uncomment the last 4 lines in the Docker file. And edit the last line, replacing <osmc IP address> with your media centre’s IP. Re-run the docker build and docker run commands. Then, at the container prompt, mount the share:
root@747fbdb287ca:/# mount /Recordings root@747fbdb287ca:/# df /Recordings Filesystem 1K-blocks Used Available Use% Mounted on //192.168.7.28/Recordings 499594916 247341468 252253448 50% /Recordings root@747fbdb287ca:/# cd /Recordings/Minder root@747fbdb287ca:/Recordings/Minder# ls Minder.2020-04-14.01-35.ts Minder.2020-04-14.15-40.ts Minder.2020-04-15.15-40.ts root@747fbdb287ca:/Recordings/Minder# comskip Minder.2020-04-14.15-40.ts ... and so on.
Exit the Container
When you are finished messing around in the Docker container, type exit or press ctrl-d.
root@747fbdb287ca:/# exit $
You are returned to the Pi command prompt. To run the container again, just repeat the docker run command above. Incidentally, the “–cap add” flags in that command are needed only so that the mount command works within the container.
Docker has different benefits in different situations. Here, it enabled us to install a software package on our system, and its fairly complicated set of dependencies, without splattering new libraries and modules all over the system. ffmpeg, libtool and other items named in the Dockerfile (under ENV PACKAGES) were installed within the Docker image, not into the Pi’s system folders. Out host system thus remains “clean” and unchanged.
When the Docker container exits, any files we created on the fly are lost, including files created by comskip. Storage is ephemeral. One solution is to use Docker storage volumes. Another is to mount a remote data area directly into the container, which is what we did.
Also, Minder one of the best programmes ever shown on TV.