Introduction
Nextcloud is a suite of client-server software for creating and using file hosting services. Nextcloud provides functionality similar to Dropbox, Office 365 or Google Drive when used with integrated office suites Collabora Online or OnlyOffice. It can be hosted in the cloud or on-premises.
PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. In 1996, the project was renamed to PostgreSQL to reflect its support for SQL.
The Docker Compose File
---
version: "3.8"
services:
postgres:
image: postgres:latest
container_name: postgres
hostname: postgres
restart: unless-stopped
security_opt:
- no-new-privileges:true
env_file:
- .env
environment:
- TZ=${TZ}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- PGUSER=${PGUSER}
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $$POSTGRES_DB -U $$POSTGRES_USER"]
start_period: 30s
interval: 10s
timeout: 10s
retries: 5
ports:
- 5432:5432
volumes:
- /opt/nextcloud/database:/var/lib/postgresql/data
- /etc/localtime:/etc/localtime:ro
networks:
- nextcloud_network
labels:
- com.centurylinklabs.watchtower.enable=true
nextcloud:
image: nextcloud:latest
container_name: nextcloud
hostname: nextcloud
restart: unless-stopped
security_opt:
- no-new-privileges:true
env_file:
- .env
environment:
- TZ=${TZ}
- POSTGRES_HOST=${POSTGRES_HOST}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ports:
- 80:80
volumes:
- /opt/nextcloud/config:/var/www/html
- /etc/localtime:/etc/localtime:ro
depends_on:
postgres:
condition: service_healthy
networks:
- nextcloud_network
labels:
- com.centurylinklabs.watchtower.enable=true
networks:
nextcloud_network:
name: nextcloud_network
external: true
docker-compose.yml
The Docker Environment Variables File
TZ=Europe/Stockholm
POSTGRES_HOST=postgres
POSTGRES_DB=db
POSTGRES_USER=user
POSTGRES_PASSWORD=password
PGUSER=user
NEXTCLOUD_ADMIN_USER=admin
NEXTCLOUD_ADMIN_PASSWORD=secretpassword
.env
This Docker Compose file defines two services, the app is named “nextcloud” and the database is called “postgres”. The services are built from the latest Docker images. The container names ares set to “nextcloud” and “postgres” as well.
The “restart” section ensures that the container will always be restarted if it exits (unless manually stopped). This is useful for ensuring that the services are always available, especially if they crash or are shut down unexpectedly.
The “security_opt” section prevents your container processes from gaining additional privileges. This is an important security consideration.
The “ports” section for Nextcloud maps port 80 on the host machine to port 80 in the container. This allows us to access the Nextcloud web interface from our local machine. The Postgres database maps to its standard port of 5432.
The “volumes” section maps the “/var/www/html” and “/var/lib/postgresql/data” directories inside the containers to local directories on the host machine. This allows us to persist data across container restarts. Nextcloud stores all its configuration data in its own directory and postgres stores its database in its own directory, so it’s important to keep it intact.
The "labels" section allows the Watchtower service to keep this container update automatically.
Running Nextcloud with Docker Compose
To run Nextcloud 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 along with the environment variables as “.env”.
Next, run the following command from the same directory:
docker compose up -d
This command will start the nextcloud container in the background and detach from it. You can then access the Nextcloud web interface by going to http://localhost in your web browser.
Conclusion
In this article, we explored how to set up and run the Nextcloud software using Docker Compose. We looked at the different sections of the Docker Compose file and explained how they work together to create a functional Nextcloud service. By running Nextcloud with Docker Compose, you can easily deploy and manage the tool on your own server, without having to worry about dependencies or configuration.

