Docker: Learn and Practice, Part 2

Docker: Learn and Practice, Part 2

If you do not know how to run Docker, you should start from Part 1.

Last time, you learn how to run a Docker container with different arguments and convert the command to a docker-compose.yml for easier maintenance. This time I will show configurations with user Docker image and talk more about different options on Docker Compose.

Portainer

Portainer is a open source Docker management web application. Although you can do all the things that it does in command line, but sometime it is nice to have GUI. This also allow given users control to manage containers without giving server login.

version: "3"

services:
  portainer:
    image: portainer/portainer
    restart: unless-stopped
    ports:
      - 9000:9000
    volumes:
      - /docker/portainer:/data
      - /var/run/docker.sock:/var/run/docker.sock

Portainer is rather simple to deploy. You only need to mount two volumes to get it running. /data contains all configurations, including users, endpoints, and etc. /var/run/docker.sock allows Portainer to manage Docker daemon of the host. If you only use it to manage remote endpoints, it is not needed.

In the YAML above, you can see restart: unless-stopped which defines the restart policy of the container.

Restart Policy

Restart policy defines what happens if the container is stopped. There are 4 policies.

  • no: If it stops, Docker does not try to restart the container.
  • always: Docker always restart the container, including restart.
  • on-failure: Only restart if it exits with non-zero code.
  • unless-stopped: Docker always restart the container unless it is stopped manually, including restart.

I would recommend unless-stopped because always restarts containers even it is stopped before Docker restart.

Plex

Plex is a self-hosted media streaming server, like your own Netflix. There are two Docker images: official and community. The community is the one being built first and the official is basically a clone.

version: "3"

services:
  plex:
    image: linuxserver/plex
    ports:
      - 32400:32400
    tmpfs: /transcode
    volumes:
      - /media:/media:ro
      - /docker/plex:/config
    environment:
      VERSION: latest

You should use network_mode: "host" instead of ports: on your first creation because Plex use LAN discovery for you to claim the server. Then, you add necessary ports afterward. As I don't use anything like DLNA, I only need the web (32400).

tmpfs is a temporary file system in memory. It will increase the responsiveness of streams and RAM usage.

Note that both versions do not follow the best practice of Docker. They download binary after the container started which defeat the purpose of reproducible build.

Netdata

Netdata is simple monitor dashboard for you to monitor host and containers status and get alert from it.

version: "3"

services:
  netdata:
    image: netdata/netdata
    hostname: example.com
    ports:
      - 19999:19999
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    cap_add:
      - SYS_PTRACE
    security_opt:
      - apparmor:unconfined

GitLab

GitLab is the most featured free self-host Git server. If you do not want to host your code on GitHub or other Git provider, then you can self-host GitLab.

version: "3"

services:
  gitlab:
    image: gitlab/gitlab-ce:latest
    privileged: true
    ports:
      - 80:80
      - 443:443
      - 22:22
      - 5000:5000
      - 5005:5005
    volumes:
      - /docker/gitlab/config:/etc/gitlab
      - /docker/gitlab/logs:/var/log/gitlab
      - /docker/gitlab/data:/var/opt/gitlab
      - /etc/letsencrypt:/ssl

Nextcloud

Nextcloud is an open source, self-hosted file share and communication platform.

version: "3"

services:
  nextcloud:
    image: nextcloud
    ports:
      - 80:80
    volumes:
      - /docker/nextcloud:/var/www/html
  mariadb:
    image: mariadb
    ports:
      - 80:80
    volumes:
      - /docker/mariadb:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: nextcloud

When you setup Nextcloud, you can connect the database through mariadb because Docker Compose create a default network and the containers in the same network can resolve other IP address by host name.

Last Word

There are many good self-host applications where you can take a look at Kickball/awesome-selfhosted. I am not able to talk about all of them.

Also, I found it is quite repetitive on how to setup different applications. You should able to setup most of them once you know how Docker works.

There will be Part 3 about how you write your own Dockerfile, build it and push it to Docker Hub.