Consider this docker-compose configuration:
# docker-compose.yml
version: "3.7"
services:
app:
build: ./app
depends_on:
- db
- vpn
ports:
- "3001:3000"
db:
image: postgres
vpn:
build: ./vpn
cap_add:
- NET_ADMIN
Description
- The
app
is accessed from the docker host via http://localhost:3001. - The
app
needs to connect to a postgresdb
, which is the second container. - Also, the
app
needs to connect to an api, which is only available though a vpn. This is why a third container,vpn
, establishes the required vpn connection.
Goal
The app
container should should be able to reach the other services within this docker-compose environment, i.e. the db
, and route the rest of its traffic through the vpn
container, such that it can access the api behind the vpn tunnel.
What I've tried
I have tried to set the
network_mode
of theapp
:services: app: network_mode: "service:vpn"
This routes all traffic of the
app
container through thevpn
. With this, I can reach the api behind the vpn tunnel from theapp
container. But this is not compatible withports: - "3001:3000"
. Also, thedb
container cannot be reached from theapp
anymore:ping: bad address 'db'
.I also have tried to link the
db
container from thevpn
container, hoping that this would make thedb
service available to theapp
.services: app: network_mode: "service:vpn" vpn: links: - db
But still
db
cannot be found byapp
.If I link the
db
container from thevpn
container but do not establish the vpn connection within thevpn
container, thedb
container can be reached from theapp
.And I've experimented with adding
127.0.0.1 db
to the/etc/hosts
of theapp
container, vaguely hoping that I could reach thedb
port directly. But this also does not work.
Does anyone have a clue how to achieve this?
app
from the host machine, what if you need to access it from another local machine ? it is not accessible then :-/ – Randolph