docker compose 没有为我的应用程序创建网络

问题描述 投票:0回答:1

我目前有一个 docker-compose.ymal 文件列出了 3 个服务:

  app1:
    image: node:12-alpine
    working_dir: /node
    command: yarn start
    volumes:
    - ./app1:/node
    ports:
    - 80
    depends_on:
    - redis
    - app1-postgres

  app2:
    image: node:12-alpine
    working_dir: /node
    command: yarn start
    volumes:
    - ./app2:/node
    ports:
    - 80
    depends_on:
    - redis
    - app1-postgres

  app1-postgres:
    image: postgres:12-alpine
    environment:
    - POSTGRES_PASSWORD=password

我想做的是让 app1 和 app2 与 app1-postgres 中的同一个数据库进行通信。当我在我的终端运行

docker-compose up
时,我得到了这些网络

NETWORK ID     NAME           DRIVER    SCOPE
3bcd2631fb23   bridge         bridge    local
17b8e4e7b05a   app1_default   bridge    local
5712287063b8   host           host      local
3de05b1f9303   none           null      local

我还希望看到 app2_default 作为我的网络之一。但是我得到了:

3de05b1f9303   none           null      local

这是否意味着我的 app2 没有与我的数据库对话?处理两个节点容器与同一个 postgres 图像对话的最有效方法是什么?

感谢您的任何见解!

postgresql docker docker-compose microservices docker-image
1个回答
-1
投票
services:
  app1:
    image: node:12-alpine
    working_dir: /node
    command: yarn start
    volumes:
    - ./app1:/node
    ports:
    - 80
    depends_on:
    - redis
    - app1-postgres
    networks:
      yournetworkname:
        ipv4_address: 172.7.0.5

  app2:
    image: node:12-alpine
    working_dir: /node
    command: yarn start
    volumes:
    - ./app2:/node
    ports:
    - 80
    depends_on:
    - redis
    - app1-postgres
    networks:
      yournetworkname:
        ipv4_address: 172.7.0.6

  app1-postgres:
    image: postgres:12-alpine
    environment:
    - POSTGRES_PASSWORD=password
    networks:
      yournetworkname:
        ipv4_address: 172.7.0.7

networks:
  yournetworkname:
    name: yournetworkname
    driver: bridge
    ipam:
      config:
        - subnet: 172.7.0.0/16
          gateway: 172.7.0.1

您必须添加网络:创建新网络,然后通过在服务中添加网络将服务连接到该网络

© www.soinside.com 2019 - 2024. All rights reserved.