Introduction
Umami is a free and open source web analytics solution that provides a simple, fast, and privacy-focused alternative to Google Analytics. You can self-host Umami on your own infrastructure using Docker, giving you complete control over your website analytics data without compromising your visitors' privacy.
The Docker Compose File
---
services:
postgres:
image: docker.io/library/postgres:alpine
container_name: postgres
hostname: postgres
restart: unless-stopped
security_opt:
- no-new-privileges:true
environment:
- TZ=Europe/Stockholm
- DATABASE_URL=postgresql://umami:umami@umami-db:5432/umami
- DATABASE_TYPE=postgresql
- APP_SECRET=replace-this-with-a-random-string
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 10s
timeout: 10s
retries: 5
ports:
- 5432:5432/tcp
volumes:
- umami-data:/var/lib/postgresql/data:rw
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
networks:
- proxy
labels:
- com.centurylinklabs.watchtower.enable=true
umami:
image: docker.io/umamisoftware/umami:latest
container_name: umami
hostname: umami
restart: unless-stopped
security_opt:
- no-new-privileges:true
environment:
- TZ=Europe/Stockholm
- POSTGRES_DB=umami
- POSTGRES_USER=umami
- POSTGRES_PASSWORD=umami
ports:
- 3000:3000/tcp
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
depends_on:
postgres:
condition: service_healthy
networks:
- proxy
labels:
- com.centurylinklabs.watchtower.enable=true
volumes:
umami-data:
external: true
networks:
proxy:
external: truedocker-compose.yml
This Docker Compose file defines two services: "umami" and "umami-db". The first service runs the Umami application itself, while the second provides the PostgreSQL database that Umami requires for storing analytics data.
The "restart" section ensures that both containers will always be restarted if they exit (unless manually stopped). This is useful for ensuring that the service is always available, especially if it crashes or is shut down unexpectedly.
The "security_opt" section prevents your container processes from gaining additional privileges. This is an important security consideration.
The "ports" section maps port 3000 on the host machine to port 3000 in the container. This allows us to access the Umami web interface from our local machine. By default, Umami runs on port 3000, but you can change it if necessary.
The "environment" section contains critical configuration for Umami. The DATABASE_URL connects Umami to the PostgreSQL database, and the APP_SECRET is used for encrypting session data. Make sure to replace the APP_SECRET with a secure random string before deploying to production.
The "volumes" section maps the database directory inside the PostgreSQL container to a local directory on the host machine. This allows us to persist data across container restarts. Umami stores all its analytics data in this database, so it's important to keep it intact.
The "depends_on" section ensures that the database is healthy before Umami starts. The healthcheck for the database verifies that PostgreSQL is ready to accept connections.
The "labels" section allows the Watchtower service to keep these containers updated automatically.
Running Umami with Docker Compose
To run Umami with Docker Compose, first, make sure you have Docker and Docker Compose installed on your machine. Then, create a new directory for your project and save the above Docker Compose file as "docker-compose.yml" in that directory.
Before starting the containers, remember to change the APP_SECRET environment variable to a secure random string. You can generate one using the following command:
openssl rand -base64 32Next, run the following command from the same directory:
docker compose up -dThis command will start both the Umami and PostgreSQL containers in the background and detach from them. You can then access the Umami web interface by going to http://localhost:3000 in your web browser.
- Username: admin
- Password: umami
Make sure to change these credentials immediately after your first login for security purposes.
Conclusion
In this article, we explored how to set up and run the Umami analytics platform using Docker Compose. We looked at the different sections of the Docker Compose file and explained how they work together to create a functional Umami service. By running Umami with Docker Compose, you can easily deploy and manage a privacy-focused web analytics tool on your own server, without having to worry about dependencies or configuration.