InstallDocker - albertmon/smarthome GitHub Wiki

Install Docker

What is docker?

On the docker site this question is answered:

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

I will try to give a simpler answer

If you want to use a computer program, you need a computer that can run that program.
If you have many programs, with different prerequisites, you can get into trouble. Especially when these prerequisites contradict each other.
Here Docker comes to the rescue.
With Docker you can build an environment exclusively for the application you want to use.
Another advantage is that you can remove the installation just by removing a docker image.

Container

In Docker terminology such an environment is called a Container.
Docker will take care of all communication to and from the container including starting and stopping.

Image

Before you can run a container, you have to build one. Just like filling a shipping container with goods you want to ship. This is called an Image. With Docker you can run an image in more than one container, on more than one computer. Another benefit of this setup: you can easily add computing power to your application.

Dockerfile

So the first step to use Docker is building Images. You can do this by yourself, by creating a Dockerfile.
In the Dockerfile you tell Docker what you need in your Image to run your application: which version of which operating system, which services or other software, which files and all other things needed in your Image. It is a kind of recipe.
Unfortunately, to create a Dockerfile you must be experienced in Docker, Linux, the application etc.

Docker Hub

Lucky for you: for many tools and programs there are ready-made Images available. You can tell Docker to run that Image just by entering a docker run command on the command line:
docker run hello-world
What happens: you tell docker to run the image hello-world. Docker can't find the image, so it looks in the standard library on Docker Hub (See:https://hub.docker.com/_/hello-world)
Docker will download the Image (pull it) and start a container with that image
As you can see on DockrHub, there are many programs that can be run with a preconfigured image.
In our SmartHome we will use docker where possible. We don't have to install all kinds of extra software. When we want another version, we can remove the container(s) and obsolete image(s) and start again.

The docker architecture looks like this: docker-architecture

On the left you see the three ways to build/pull/run images and containers using the docker command

  • docker build
    Create an image from a Dockerfile and store it in the docker environment
  • docker pull
    Create an image by downloading it from the Docker Hub
  • docker run
    Run an image in a container (does a pull when the image is not available locally)
    The container is run by the docker daemon, more than one container from the same image can be run!

In the middle you see the docker daemon and its parts
On the right you see the registry (Docker Hub) where ready-to-use images are available for download

See also: Docker Cheat Sheet


Docker Install

The original full install manual for the Raspberry Pi can be found at
https://docs.docker.com/engine/install/debian/

The simple one is (assuming no docker is installed):
Docker provides a convenience script to install Docker into development environments quickly and non-interactively. We will use it for our production environment.
Always examine scripts downloaded from the internet before running them locally. Before installing, make yourself familiar with potential risks and limitations of the convenience script.

  1. Download the convenience script and save it as get-docker.sh.
    This is a shell-script that does all the work.
    You can download it at https://get.docker.com
    Execute the following code:
    curl -fsSL https://get.docker.com -o get-docker.sh
  2. (Optional) Execute the script to see what will be done (Dry-run)
    DRY_RUN=1 sh ./get-docker.sh
  3. Execute the script as root to do the real install
    sudo sh get-docker.sh
    The script installs dependencies and recommendations without asking for confirmation. This may install a large number of packages.
    Docker is installed. The docker service starts automatically on Raspberry Pi (Debian based distribution).

Note:
Docker is running with root privileges.
This can have a severe security impact if your container can access files outside the container.
Always be aware of the consequences.

If you want to run and manage docker as non-root user (e.g. pi) without using sudo, execute the next commands:

  1. Create a group docker
    sudo groupadd docker
  2. Add yourself (user pi) to the docker group:
    sudo usermod -aG docker $USER

Before you can use docker without sudo you have to logout and login again (or run newgrp docker)

  1. (Optional) Test docker by running Hello World. This is a simple docker image in the Docker Hub:
    docker run hello-world

Docker-compose Install

See also: https://docs.docker.com/compose/

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
The simplest way to install docker-compose is using python pip.

Just type:

sudo pip3 install docker-compose