# Deploy Nextcloud with docker compose - (Medium site)

Link: [https://chrisgrime.medium.com/deploy-nextcloud-with-docker-compose-935a76a5eb78](https://chrisgrime.medium.com/deploy-nextcloud-with-docker-compose-935a76a5eb78) 8 Setembro 2023.

In the spirit of DIY, experimenting, and learning, I set up a Nextcloud server to replace Google Drive and One Drive. Over the years my Nextcloud instance has become home to many of my backed up files, contacts, calendar, notes, and my server even has an office suite thanks to Collabora.

Nextcloud can be a pretty awesome, open source alternative to many services.

For better or for worse, I decided to deploy Nextcloud on my home server using a Docker container. If you’re looking to get experience in Docker or trying to find out how to set up a Docker Compose file to deploy Nextcloud you’re in the right spot.

The docker-compose.yml

```
---<br></br>version: '3'<br></br><br></br>services:<br></br>  nextcloud:<br></br>    image: nextcloud<br></br>    container_name: nextcloud<br></br>    restart: unless-stopped<br></br>    networks: <br></br>      - cloud<br></br>    depends_on:<br></br>      - nextclouddb<br></br>      - redis<br></br>    ports:<br></br>      - 8081:80<br></br>    volumes:<br></br>      - ./html:/var/www/html<br></br>      - ./custom_apps:/var/www/html/custom_apps<br></br>      - ./config:/var/www/html/config<br></br>      - ./data:/var/www/html/data<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>      - MYSQL_DATABASE=nextcloud<br></br>      - MYSQL_USER=nextcloud<br></br>      - MYSQL_PASSWORD=dbpassword<br></br>      - MYSQL_HOST=nextclouddb<br></br>      - REDIS_HOST=redis<br></br><br></br>  nextclouddb:<br></br>    image: mariadb<br></br>    container_name: nextcloud-db<br></br>    restart: unless-stopped<br></br>    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW<br></br>    networks: <br></br>      - cloud<br></br>    volumes:<br></br>      - ./nextclouddb:/var/lib/mysql<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>      - MYSQL_RANDOM_ROOT_PASSWORD=true<br></br>      - MYSQL_PASSWORD=dbpassword<br></br>      - MYSQL_DATABASE=nextcloud<br></br>      - MYSQL_USER=nextcloud<br></br>      <br></br>  collabora:<br></br>    image: collabora/code<br></br>    container_name: collabora<br></br>    restart: unless-stopped<br></br>    networks: <br></br>      - cloud<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>      - password=password<br></br>      - username=nextcloud<br></br>      - domain=example.com<br></br>      - extra_params=--o:ssl.enable=true<br></br>    ports:<br></br>      - 9980:9980<br></br><br></br>  redis:<br></br>    image: redis:alpine<br></br>    container_name: redis<br></br>    volumes:<br></br>      - ./redis:/data  <br></br>    networks: <br></br>      - cloud<br></br>  <br></br>  nginx-proxy:<br></br>    image: 'jc21/nginx-proxy-manager:latest'<br></br>    container_name: nginx-proxy<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>    restart: unless-stopped<br></br>    ports:<br></br>      - '80:80'<br></br>      - '81:81'<br></br>      - '443:443'<br></br>    volumes:<br></br>      - ./data:/data<br></br>      - ./letsencrypt:/etc/letsencrypt<br></br><br></br>networks:<br></br>  cloud:<br></br>    name: cloud<br></br>    driver: bridge
```

Alright. Lets break this down.

This Docker Compose file will deploy 5 containers. They are:

1. Nextcloud
2. MySQL database required for Nextcloud
3. Collabora — A awesome open source office suite similar to google docs. Collabora Office also has mobile apps.
4. Redis — Memory Caching. If you’re going to rely on next cloud for important files, I highly recommend setting up Redis.
5. Nginx Proxy Manager — A reverse proxy manager to handle incoming requests to the server.

## Docker Compose

A little explanation for what each line does.

Nextcloud:

```
nextcloud:<br></br>    image: nextcloud # The image that will be used. The official nextcloud docker<br></br>    container_name: nextcloud # Just the name of the container. Help you identify it<br></br>    restart: unless-stopped # If something happens like the container crashes then we want the container to start up again<br></br>    networks: # Link all the containers through the network "cloud"<br></br>      - cloud<br></br>    depends_on: # Wait for the database and redis containers before starting nextcloud<br></br>      - nextclouddb <br></br>      - redis<br></br>    ports: # If you have multiple web service on your server you need to change the port. I am directing nextcloud from port 80 to port 8081<br></br>      - 8081:80<br></br>    volumes: # These are important. This will map a file directory inside the container to a directory on your actual computer<br></br>      - ./html:/var/www/html # Map the /var/www/html directory in the container to the html folder in the same folder as the docker-compose.yml<br></br>      - ./custom_apps:/var/www/html/custom_apps # These volumes allow us to easily interact with the files in the container<br></br>      - ./config:/var/www/html/config<br></br>      - ./data:/var/www/html/data<br></br>    environment:<br></br>      - PUID=1000 # The user ids. Most likely both should be 1000. Incorrectly setting these will led to file permission issues<br></br>      - PGID=1000 # Set these to whatever your user is.<br></br>      - TZ=America/Los_Angeles # Set this to your timezone<br></br>      - MYSQL_DATABASE=nextcloud # This is the database information we will set up in the next section<br></br>      - MYSQL_USER=nextcloud<br></br>      - MYSQL_PASSWORD=dbpassword<br></br>      - MYSQL_HOST=nextclouddb<br></br>      - REDIS_HOST=redis # The redis container to use
```

Nextcloud Database:

```
nextclouddb:<br></br>    image: mariadb # offical mariadb image<br></br>    container_name: nextcloud-db <br></br>    restart: unless-stopped <br></br>    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW # I honestly cant remember. If you know why let me know.<br></br>    networks: <br></br>      - cloud<br></br>    volumes:<br></br>      - ./nextclouddb:/var/lib/mysql<br></br>    environment:<br></br>      - PUID=1000 # Should be the same as the other containers<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>      - MYSQL_RANDOM_ROOT_PASSWORD=true<br></br>      - MYSQL_PASSWORD=dbpassword # Same information that was entered in the nextcloud portion<br></br>      - MYSQL_DATABASE=nextcloud<br></br>      - MYSQL_USER=nextcloud
```

Collabora (Optional, but really cool):

```
collabora:<br></br>    image: collabora/code:latest<br></br>    container_name: collabora<br></br>    restart: unless-stopped<br></br>    networks: <br></br>      - cloud<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles # Should be the same as the others <br></br>      - password=password <br></br>      - username=nextcloud<br></br>      - domain=example.com # domain your nextcloud is on<br></br>      - extra_params=--o:ssl.enable=true # Use if have ssl. You should.<br></br>    ports:<br></br>      - 9980:9980
```

Redis (Optional, but seriously, just add it. You will run into file lock issues eventually and Redis will prevent it):

```
redis:<br></br>    image: redis:alpine<br></br>    container_name: redis<br></br>    volumes:<br></br>      - ./redis:/data  <br></br>    networks: <br></br>      - cloud
```

Nginx Proxy Manager:

```
nginx-proxy:<br></br>    image: 'jc21/nginx-proxy-manager:latest'<br></br>    container_name: nginx-proxy<br></br>    environment:<br></br>      - PUID=1000<br></br>      - PGID=1000<br></br>      - TZ=America/Los_Angeles<br></br>    restart: unless-stopped<br></br>    ports:<br></br>      - '80:80'<br></br>      - '81:81'<br></br>      - '443:443'<br></br>    volumes:<br></br>      - ./data:/data<br></br>      - ./letsencrypt:/etc/letsencrypt
```

Nginx Proxy manager is an awesome program that will direct incoming traffic to the server towards Nextcloud. Add a new host.

On the details panel enter your domain name.

Scheme = http, Forward Hostname = whatever your machines local IP address is, (for example 192.168.1.35) and forward port = 80.

Cache Assets, Block Common Exploits, Websockets support all should be on.

Next on the Custom Locations tab we will enable caldav and carddav to allow remote access to your calendars and contacts.

Location 1:

1. location = /.well-known/caldav
2. scheme = html
3. Forward Hostname = &lt;local IP&gt;/ remote.php/dav
4. Forward Port 80

Location 2:

1. location = /.well-known/carddav
2. scheme = html
3. Forward Hostname = &lt;local IP&gt;/ remote.php/dav
4. Forward Port 80

## Additional Configuration

Now if you are setting up Nextcloud to work with your custom domain you will need to open the config.php file and change trusted domains to whatever your domain is.

If you are trying to access your Nextcloud from your network it could be useful to add your Nextcloud’s local IP address.

```
'trusted_domains' => <br></br> array (<br></br> 0 => 'example.com',<br></br> 1 => '192.168.1.12:8081',<br></br> ),<br></br> 'overwritehost' => 'example.com',<br></br> 'overwriteprotocol' => 'https',
```

Since Niginx Proxy Manager is set up, the following needs to be added to the config.php file:

```
'default_phone_region' => 'US',<br></br> 'trustedproxies' => <br></br> array (<br></br> 0 => 'NginxProxyManager',<br></br> 1 => '192.168.0.145',<br></br> ),
```

To solve some of the warnings you need to do the following:

```
 ‘default_phone_region’ => ‘US’,
```

To set up mail alerts you will need to add the following to the config file. The values will need to be obtained from your email provider.

```
 'mail_from_address' => 'user', # insert your emails user<br></br> 'mail_smtpmode' => 'smtp',<br></br> 'mail_sendmailmode' => 'smtp',<br></br> 'mail_domain' => 'example.com', # Your email domain<br></br> 'mail_smtphost' => 'smtp.example.com', <br></br> 'mail_smtpport' => '465',<br></br> 'mail_smtpauth' => 1,<br></br> 'mail_smtpsecure' => 'ssl',<br></br> 'mail_smtpname' => 'user@example.com',<br></br> 'mail_smtppassword' => 'secretpassword',
```

## Run the container

```
docker-compose up -d
```

Congratulations! Nextcloud is setup using docker containers and docker compose! Let me know if you have any questions.

## Potential Issues

- If you run into a 502 Gateway Error try clearing the cookies in your browser for the domain your server is hosted. It works most of the time for me.
- Make sure to update the docker images on a regular basis. Nextcloud in a Docker cannot whole number skip versions. For example if your version is 24 and the newest version is 26, **DO NOT** update straight to 26. I learned this the hard way. Update first to 25. So run “docker-compose pull” somewhat regularly.