gpt4 book ai didi

Docker 组成容器通信

转载 作者:行者123 更新时间:2023-12-02 20:42:25 25 4
gpt4 key购买 nike

我有 7 个 docker 容器

  1. es_search
  2. postgres_db
  3. 融合验证
  4. mysql数据库
  5. 授权
  6. 后端
  7. 用户界面

容器之间的通信应该如下所示

  • fusionauth 应该可以联系 es_search, postgres_db
  • backend应该可以联系auth,mysql_db
  • auth 应该可以联系到 fusionauth,后端
  • ui 应该可以联系后端,auth

现有的 docker-compose

version: '3.1'

services:
postgres_db:
container_name: postgres_db
image: postgres:9.6
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- ${POSTGRES_PORT}:5432
networks:
- postgres_db
restart: unless-stopped
volumes:
- db_data:/var/lib/postgresql/data_test

es_search:
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.1
container_name: es_search
environment:
- cluster.name=fusionauth
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=${ES_JAVA_OPTS}"
ports:
- ${ES1_PORT}:9200
- ${ES_PORT}:9300
networks:
- es_search
restart: unless-stopped
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- es_data:/usr/share/elasticsearch/data

fusionauth:
image: fusionauth/fusionauth-app:latest
container_name: fusionauth
depends_on:
- postgres_db
- es_search
environment:
DATABASE_URL: jdbc:postgresql://postgres_db:5432/fusionauth
DATABASE_ROOT_USER: ${POSTGRES_USER}
DATABASE_ROOT_PASSWORD: ${POSTGRES_PASSWORD}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
FUSIONAUTH_MEMORY: ${FUSIONAUTH_MEMORY}
FUSIONAUTH_SEARCH_SERVERS: http://es_search:9200
FUSIONAUTH_URL: http://fusionauth:9010
networks:
- postgres_db
- es_search
restart: unless-stopped
ports:
- ${FUSIONAUTH_PORT}:9011
volumes:
- fa_config:/usr/local/fusionauth/config

db:
container_name: db
image: mysql:5.7
volumes:
- /etc/nudjur/mysql_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
ports:
- ${MYSQL_PORT}:3306
command: --default-authentication-plugin=mysql_native_password
restart: on-failure

backend:
container_name: backend
links:
- db:${MYSQL_HOST}
depends_on:
- db
image: ${BACKEND_IMAGE}
volumes:
- ${ENV_FILE}:/backend/.env
ports:
- ${BACKEND_PORT}:${BACKEND_PORT}
command: >
bash -c "set -a && source .env && set +a"
restart: unless-stopped

UI:
container_name: UI
image: ${UI_IMAGE}
volumes:
- ${ENV_FILE}:/nudjur/.env
ports:
- ${UI_PORT}:${UI_PORT}
command: >
bash -c "PORT=${UI_PORT} npm start"
restart: unless-stopped

auth:
container_name: auth
network_mode: host
image: ${AUTH_IMAGE}
volumes:
- ${ENV_FILE}:/auth/.env
ports:
- ${AUTH_PORT}:${AUTH_PORT}
command: >
bash -c "set -a && source .env && set +a && python3 ./auth_bridge.py --log-level DEBUG run -p ${AUTH_PORT}"
restart: unless-stopped

networks:
postgres_db:
driver: bridge
es_search:
driver: bridge

volumes:
db_data:
es_data:
fa_config:


我对如何在它们之间建立通信感到困惑。

有人可以帮我解决这个问题吗?

最佳答案

我知道您想限制通信,以便容器只能与其他服务通信,例如:

  • fusionauth should be able to contact es_search, postgres_db
  • backend should be able to contact auth, mysql_db
  • auth should be able to contact fusionauth, backend
  • ui should be able to contact backend, auth

您可以使用 networks正如您在示例中已经部分执行的那样以启用通信,例如:

  • 同一网络上的服务可以使用服务名称或别名相互访问 - 即 es_search 可以通过同一网络上的其他服务通过 http://es_search:9200< 访问
  • 不同网络上的服务相互隔离,无法相互通信

然后您可以定义您的网络,例如:

services:

postgres_db:
networks:
- postgres_db

es_search:
networks:
- es_search

# fusionauth should be able to contact es_search, postgres_db
fusionauth:
networks:
- fusionauth
- postgres_db
- es_search

db:
networks:
- mysql_db

# backend should be able to contact auth, mysql_db
backend:
networks:
- backend
- auth
- mysql_db

# ui should be able to contact backend, auth
UI:
networks:
- backend
- auth

# auth should be able to contact fusionauth, backend
auth:
networks:
- auth
- fusionauth
- backend


networks:
fusionauth:
backend:
auth:
postgres_db:
es_search:
mysql_db:

所有服务(ui 除外)都有自己的网络,另一个服务必须在该服务的网络上才能与之通信。

注意:我没有使用 link因为它是遗留的,可能会在未来的版本中删除,如文档所述。

关于Docker 组成容器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61244829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com