How to set up a docker compose stack with wireguard and multiple services?

263 Views Asked by At

I'm trying on getting a nextcloud docker compose stack to work with wireguard. The idea is having a WG server running on a VPS and connecting a nextcloud stack to it, to forward all the traffic via e.g. the nginx proxy manager.

The connection does work well and I can ping the stack from the server side. Also http requests reach the apache server. Unfortunately this ends in an 500: Internal Server error:

nextcloud-docker-vpn-app-1    | 192.168.6.1 - - [25/Nov/2023:12:17:20 +0000] "GET / HTTP/1.1" 500 4232 "-" "Wget/1.21.2"

The nextcloud server logs in the container on the other hand tell me, that the database server cannot be reached: "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name \"db\" to address: Name or service not known"

So it seems that the name resolution is not working in my compose setup. Has anyone a solution for this problem?

My docker-compose file is as follows:

version: '3'

services:
  db:
    image: postgres:alpine
    restart: always
    volumes:
      - db:/var/lib/postgresql/data:Z
    env_file:
      - db.env
    network_mode: service:wireguard

  redis:
    image: redis:alpine
    restart: always
    network_mode: service:wireguard

  app:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    environment:
      - POSTGRES_HOST=nextcloud-db
      - REDIS_HOST=redis
    env_file:
      - db.env
    depends_on:
      - db
      - redis
      - wireguard
    network_mode: service:wireguard

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

  wireguard:
    image: linuxserver/wireguard
    container_name: wireguard
    restart: unless-stopped
    volumes:
      - './wireguard:/config'
      - '/lib/modules:/lib/modules:ro'
    environment:
      - PUID=1000
      - PGID=1000
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
      - net.ipv6.conf.all.disable_ipv6=1
    ports:
      - 80:80
      - 41194:51820/udp

volumes:
  db:
  nextcloud:

0

There are 0 best solutions below