Install Docker and docker-compose on Raspberry Pi

Installing Docker requires adding the a repository with the updated version:

sudo apt update
sudo apt upgrade
sudo apt install apt-transport-https \
                 ca-certificates \
                 software-properties-common \
curl -fsSL https://yum.dockerproject.org/gpg | sudo apt-key add -
apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609D
sudo nano /etc/apt/sources.list

Add the line:

deb https://apt.dockerproject.org/repo/ debian-stretch main

changing ‘stretch‘ with your version of Raspbian. You can check your version using lsb_release -cs.

sudo apt update
curl -sSL https://get.docker.com | sh
sudo usermod -aG docker pi
sudo reboot

For docker-compose, first we will need to install Berry Conda:

wget https://github.com/jjhelmus/berryconda/releases/download/v2.0.0/Berryconda3-2.0.0-Linux-armv7l.sh
chmod +x Berryconda3-2.0.0-Linux-armv7l.sh
./Berryconda3-2.0.0-Linux-armv7l.sh
sudo reboot

And then install docker-compose using pip:

pip install docker-compose
sudo reboot

Run Mongo from a docker

To create the container:

docker run --name mongo -v /home/core/mongo-files/data:/data/db -v /home/core/mongo-files:/opt/keyfile --hostname="node1.example.com" -p 27017:27017 -d mongo:2.6.5 --smallfiles

(remember to change 2.6.5 with the version you want to use)

To run the container:

docker start mongo

For accessing Mongo terminal:

docker exec -it [NameOfTheContainer] mongo

Therefore, in this case:

docker exec -it mongo mongo

More information here and in the official Docker image documentation. This script example is also interesting.

Managing docker images and containers

Docker. Probably one of the best inventions since the sliced bread. It makes the life sooooo easy.

Some useful commands:

List images

docker images

Remove images

docker rmi  <IMAGE ID>

List containers

docker ps -a

Remove containers

docker rm  <CONTAINER ID>

Stop all containers

docker stop $(docker ps -a -q)

Remove all containers

docker rm $(docker ps -a -q)

Accessing to a container’s console

docker exec -it <name of the container> bash

Easy.