首先先配置好桥接容器(mysql_bridge 为一个桥接网络,A容器可以在此暴露A容器的服务接口,让B容器使用A容器的服务)
docker network mysql_bridge
直接上
version: '3' services:
mysql:
container_name: mysql
image: mysql:5.7.21
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./data/mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
networks:
- mysql_bridge
restart: on-failure redis:
container_name: redis
image: redis:4.0.6
expose:
- 6379
ports:
- 6379:6379
volumes:
- ./data/redis:/usr/share/redis/data:rw
networks:
- redis_bridge mongo:
container_name: mongo
image: mongo:4.0.9
volumes:
- ./data/mongo:/data/db
expose:
- 27017
ports:
- 27017:27017
networks:
- mongo_bridge
restart: on-failure networks:
mysql_bridge:
external: true
redis_bridge:
external: true
mongo_bridge:
external: true
容器使用桥接例子:
version: '3' services:
web:
container_name: web
build:
context: ./
volumes:
- ./:/var/www
ports:
- 81:80
external_links:
- mysql
- redis
restart: on-failure
networks:
- mysql_bridge
- redis_bridge networks:
mysql_bridge:
external: true
redis_bridge:
external: true