简体   繁体   中英

Docker communication inside docker compose and with database which is outside docker

I'm little bit confused with docker and network communication. I tried many things but it didn't work :-(.

I have following docker compose:

version: '3'
  services:
    nginx:
      container_name: nginx
      image: nginx:stable-alpine
      restart: unless-stopped
      tty: true
      ports:
        - 80:80
      volumes:
        - ./nginx/conf.d:/etc/nginx/conf.d:ro
      depends_on:
        - app
      networks:
        - frontend
        - backend
    app:
      restart: unless-stopped
      tty: true
      build:
        context: .
        dockerfile: Dockerfile
      container_name: app
      expose:
        - "9090"
      ports:
        - 9090:9090
      networks:
        - backend

networks:
  frontend:
  backend:

And I would like to communicate:

  1. From nginx to app //this probably works
  2. From app to postgreSQL which is installed on server (no docker container)

I cannot do this, I tried many things but something is wrong :-(

You can choose any of these two options:

  1. Make your postgresql listen to all your network interfaces (or the docker bridge for more secure but complex setup), to achieve that you need to make sure your config looks like this:
# grep listen /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
  1. Use host network mode in your docker compose, which runs docker in your host network name space instead of creating a new network:
network_mode: "host"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM