# Docker configuration

Docker was used to install in a separate container influxdb, grafana and a docker management app called portainer, which allows the management of different docker containers through a graphical interface.

## Docker-compose

Portainer allows the execution of a docker-compose code, in order to create a stack that contains influxdb and grafana.

The docker-composed installed is:

```bash
version: '3'
services:
  influxdb:
    image: influxdb:latest
    ports:
      - '8087:8086'
    restart: always
    volumes:
      - /docker/influxdb_dtman/var/lib/influxdb2:/var/lib/influxdb2
      - /docker/influxdb_dtman/etc/influxdb2:/etc/influxdb2/config.yml
      - /etc/letsencrypt/live/digitaltwin.polito.it/fullchain.pem:/etc/letsencrypt/certs/digitaltwin.polito.it/fullchain.pem
      - /etc/letsencrypt/live/digitaltwin.polito.it/privkey.pem:/etc/letsencrypt/certs/digitaltwin.polito.it/privkey.pem
    environment:
      - INFLUXD_TLS_CERT=/etc/letsencrypt/certs/digitaltwin.polito.it/fullchain.pem
      - INFLUXD_TLS_KEY=/etc/letsencrypt/certs/digitaltwin.polito.it/privkey.pem
  grafana:
    image: grafana/grafana:latest
    ports:
      - '3001:3000'
    restart: always
    user: '0'
    volumes:
      - /docker/grafana_dtman/var/lib/grafana:/var/lib/grafana
      - /etc/letsencrypt/live/digitaltwin.polito.it/fullchain.pem:/etc/letsencrypt/certs/digitaltwin.polito.it/fullchain.pem
      - /etc/letsencrypt/live/digitaltwin.polito.it/privkey.pem:/etc/letsencrypt/certs/digitaltwin.polito.it/privkey.pem

    environment:
      - GF_SERVER_CERT_FILE=/etc/letsencrypt/certs/digitaltwin.polito.it/fullchain.pem
      - GF_SERVER_CERT_KEY=/etc/letsencrypt/certs/digitaltwin.polito.it/privkey.pem
      - GF_SERVER_PROTOCOL=https
    depends_on:
      - influxdb
```

* **Version 3** of Docker-compose

* **Services**: allows to create or update different containers, in our case they are: influxdb and grafana.

* **Influxdb Service**: Download the latest influxdb image on docker hub.
    * Port 8086 of the container listens to port 8087 of the virtual machine.
    * Restart always.
    * Volumes: 
        * The virtual machine's /docker/influxdb/var/lib/influxdb2 directory is mapped to the container's /var/lib/influxdb2 directory, and inside /docker/influxdb/etc/influxdb2 is mapped the config file.
        * The fullchain.pem and privkey.pem are mapped into the container to verify the letsencrypt certificate.
    * Environemnt variables for SSL certificate.

* **Grafana Service**: Download the latest image of grafana on docker hub.
    * Container port 3000 listens to virtual machine port 3001.
    * Restart always.
    * User '0' represents running the app as root.
    * Volumes: The virtual machine's /docker/grafana/var/lib/grafana directory is mapped to the container's /var/lib/grafana directory.
    * The fullchain.pem and privkey.pem are mapped into the container to verify the letsencrypt certificate.
    * Environemnt variables for SSL certificate.
    * depends_on: In this way grafana recognizes the InfluxDB container, allowing direct communication between them.