It's nice to have a docker environment laying around, but wouldn't be better to monitor all your containers? With this few and simple commands it's done quickly with just 3 containers (cAdvisor, Prometheus, Grafana).
cAdvisor (Container Advisor) is the first tool that we're gonna using, it is a running daemon that collects, aggregates, processes, and exports information about running containers. Specifically, for each container it keeps resource isolation parameters, historical resource usage, histograms of complete historical resource usage and network statistics. This data is exported by container and machine-wide. To run the Image from the Docker Hub, we will use the following command:
docker run --name cadvisor -d -p 8005:8080 --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --restart=always google/cadvisor:latest
This will run cAdvisor on Port 8005 and restarts the container automatically if the host is powered off. As long as cAdvisor don't stores the data from the containers for a long time, we need to create some Folders on the host system (another way is to create volumes for that, but we want to keep it as simple as possible at this point) to save it via the next tools there.
mkdir /docker mkdir /docker/prometheus mkdir /docker/prometheus-data mkdir /docker/grafana-data nano /docker/prometheus/prometheus.yml
This is the content that must be inside of the
global: scrape_interval: 15s evaluation_interval: 15s external_labels: monitor: 'docker-host-alpha' rule_files: - "targets.rules" - "host.rules" - "containers.rules" scrape_configs: - job_name: 'cadvisor' scrape_interval: 5s static_configs: - targets: ['cadvisor:8080'] - job_name: 'prometheus' scrape_interval: 10s static_configs: - targets: ['localhost:9090']
Because the grafana and prometheus container's must have access to the /docker folder we set the permissions recursively
chmod -R 777 /docker.
Now we can start prometheus, which will collect and store the data provided by cAdvisor, on port 9090 and give it access to the cAdvisor container:
docker run --name prometheus --link cadvisor:cadvisor -d -p 9090:9090 --volume=/docker/prometheus/:/etc/prometheus/ --volume=/docker/prometheus-data:/prometheus -e config.file=/etc/prometheus/prometheus.yml -e storage.tsdb.path=/prometheus -e web.console.libraries=/etc/prometheus/console_libraries -e web.console.templates=/etc/prometheus/consoles -e storage.tsdb.retention=200h --restart=always prom/prometheus:latest
Yay, there's not much left. We can start grafana on port 3000 and give it access to the prometheus container:
docker run --name grafana --link prometheus:prometheus -d -p 3922:3000 --volume=/docker/grafana-data:/var/lib/grafana --restart=always grafana/grafana:latest
We're ready to navigate to grafana in our browser (https://YOUR-IP:3922/) and add Prometheus as Datasource. The default username for the grafana login is admin and the password is also admin.
To view our data we could start making our own dashboard or use some prebuild dashboard (and customize it if we want to). In our case we will use the dashboard from philicious with the dashboard ID 193. To add it to our grafana instance we have just to click on "import dashboard".
That's all! Now we have a fancy dashboard to monitor our container landscape.