brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: network_mode service: sends a Compose service through another container's network

Docker
~/tips/tip-docker-compose-network-mode-service

services:

Let one service share the network stack of another, such as a VPN container.

Download clients behind a VPN container are the usual example: the app should have no network of its own and use only the VPN container’s connection. In Compose that is one line.

services:
  vpn:
    image: example/vpn:1.0
    cap_add:
      - NET_ADMIN
    ports:
      - "8080:8080"
  client:
    image: example/client:1.0
    network_mode: "service:vpn"

The client container joins the network namespace of vpn. It has the same IP address and the same interfaces, so its traffic leaves through the tunnel. Compose also adds a dependency on vpn for you, which you can see in docker compose config.

Note where the port is. The client’s web UI listens on 8080 inside the shared namespace, so the port has to be published on the vpn service. A ports: entry on client fails when the container is created, with an error about conflicting options: port publishing and the container type network mode.

When the VPN container is recreated, the client is left attached to a namespace that no longer exists and loses its network. Compose marks the generated dependency with restart: true, so a docker compose up -d restarts the client as well. If you restart things by hand, restart the client after the VPN. I would also check from inside the client that the public IP really is the VPN’s before trusting it.

Further reading: Compose file reference: network_mode

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link