How to install NextCloud via Docker
Install Docker
Create a Docker Compose file
Once you have Docker and Docker Compose installed, create a new file called docker-compose.yml
. You can do this in any text editor of your choice. In this file, we will define the Nextcloud service and its dependencies.
version: '3'
services:
nextcloud:
image: nextcloud:fpm
container_name: nextcloud
restart: unless-stopped
volumes:
- ./nextcloud:/var/www/html
depends_on:
- nextcloud_db
- nextcloud_redis
environment:
MYSQL_HOST: db
MYSQL_DATABASE: nextcloud
MYSQL_USER: nextcloud
MYSQL_PASSWORD: nextcloud
networks:
- net
nextcloud_db:
image: mariadb
container_name: nextcloud_db
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: unless-stopped
volumes:
- ./db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: nextcloud_r_pass
MYSQL_DATABASE: nextcloud
MYSQL_USER: nextcloud
MYSQL_PASSWORD: nextcloud
networks:
- net
nextcloud_redis:
image: "redis:alpine"
container_name: nextcloud_redis
command: redis-server --appendonly yes --save 20 1 --loglevel warning
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
start_period: 20s
interval: 30s
retries: 5
timeout: 3s
expose:
- 6379
mem_limit: 2048m
mem_reservation: 512m
volumes:
- ./redis:/data
environment:
- REDIS_REPLICATION_MODE=master
networks:
- net
caddy:
image: caddy
container_name: caddy
ports:
- "80:80"
- "443:443"
environment:
- ACME_AGREE=true
restart: unless-stopped
depends_on:
- nextcloud
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./caddy_data:/data
- ./caddy_config:/config
- ./nextcloud:/var/www/html
networks:
- net
networks:
net:
So what does this docker compose file do?
This file defines four services, nextcloud_db
, nextcloud
, nextcloud_redis
and caddy
the reverse proxy. The nextcloud_db
service uses the mariadb
image to run a MariaDB database container. It also sets the necessary environment variables. The nextcloud
service uses the nextcloud
image to run the Nextcloud application container. the nextcloud_redis
is used for significantly improving your Nextcloud server performance with memory caching, where frequently-requested objects are stored in memory for faster retrieval
Create Caddyfile
example.com {
redir /.well-known/carddav /remote.php/dav 301
redir /.well-known/caldav /remote.php/dav 301
@forbidden {
path /.htaccess
path /data/*
path /config/*
path /db_structure
path /.xml
path /README
path /3rdparty/*
path /lib/*
path /templates/*
path /occ
path /console.php
}
respond @forbidden 404
root * /var/www/html
php_fastcgi nextcloud:9000
file_server
}
Change example.com to your domain or subdomain.
Bring up the docker containers
docker compose up -d && docker compose logs -f
This command will start the nextcloud container in detached mode, meaning it will run in the background. and it will show you the logs from nextcloud so you can make sure there were no errors. hit CTRL + C to close the logs.
Stop containers temporarily
This will stop the containers but not delete the temporary network, or remove temporary files etc.
docker compose stop
Bring down the containers completely
This will stop and remove the containers, but keep the data in the volumes mentioned in docker-compose.yml intact so that your data is not lost.
docker compose down
That’s it! You’ve now installed Nextcloud using Docker Compose. With Docker Compose, you can easily manage and deploy complex applications like Nextcloud with just a few commands.