Networking Problem - Connecting containers in same docker-compose file

I have a docker compose stack with two containers using Portainer.
I want the second container to use the network of the first one, but when I deploy the stack, I am getting errors such as:

Error response from daemon: conflicting options: port publishing and the container type network mode

version: "3"
services:
  gluetun:
    container_name: gluetun
    image: qmcgaw/gluetun
    cap_add:
      - NET_ADMIN
    ports:
      - 8080:8080 # port for qbittorrent      
    environment:
      - VPN_SERVICE_PROVIDER=redacted
      - OPENVPN_USER=redacted
      - OPENVPN_PASSWORD=redacted
      - SERVER_COUNTRIES=redacted  
# qbittorrent ----------------------------------------------------
  qbittorrent:
    image: lscr.io/linuxserver/qbittorrent:latest
    container_name: qbittorrent
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Australia/Sydney
      - WEBUI_PORT=8080
    volumes:
      - ./config:/config
      - ./downloads:/downloads
    ports:
      - 6881:6881
      - 6881:6881/udp
    restart: unless-stopped
    network_mode: service:gluetun

I think I’ve followed the advice on the gluetun github correctly:

Removing the last line will successfully deploy the stack.
I don’t understand what I’ve done wrong.

TIA

When your qbittorrent service uses the network namespace of the gluetan service, the owner of the network namespace is gluetan. As such, ports must be published on the gluetan service, no on the qbittorrent service.

1 Like

Thank you kindly, that makes sense. I updated the docker compose file accordingly and it’s running now.