简体   繁体   English

docker compose 内部的 Docker 通信以及与 docker 外部的数据库的通信

[英]Docker communication inside docker compose and with database which is outside docker

I'm little bit confused with docker and network communication.我对 docker 和网络通信有点困惑。 I tried many things but it didn't work :-(.我尝试了很多东西,但没有用:-(。

I have following docker compose:我有以下 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从 nginx 到应用程序 //这可能有效
  2. From app to postgreSQL which is installed on server (no docker container)从应用程序到安装在服务器上的 postgreSQL(无 docker 容器)

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:让你的 postgresql 监听你所有的网络接口(或者更安全但更复杂的设置的 docker 桥接器),为了实现这一点,你需要确保你的配置看起来像这样:
# 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:在您的 docker compose 中使用主机网络模式,它在您的主机网络名称空间中运行 docker 而不是创建新网络:
network_mode: "host"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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