1、创建一个网络
docker network create-d bridge ${netName}
2、编写docker-compose.yaml文件,指定网络,例如启动mysql
version: '2'
services:
mysql81:
image: -
restart: always
volumes:
- ./conf:/etc/mysql
- ./db:/var/lib/mysql
ports:
- "12306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootroot
TZ: Asia/Shanghai
tty: true
networks:
default:
external:
name: ${netName}
修改网络之后,一定对容器进行重新构建,运行docker-compose down;docker-compose up -d;
3、编写需要链接mysql81容器的docker-compose.yaml文件,网路指定同一个,设置链接的容器,代码如下
不同的yaml配置:
version: '3'
services:
iotmqtt:
image: -
restart: always
external_links:
- redis --- yaml的容器名:别名
- mos:mqtt
command: /root/auto_service.sh
tty: true
networks:
default:
external:
name: ${netName}
同一个yaml配置
version: '3'
services:
iotmqtt:
image: -
restart: always
links:
- redis --- yaml的容器名:别名
- mos:mqtt
command: /root/auto_service.sh
tty: true
networks:
default:
external:
name: ${netName}
4、测试联通
docker exec -it 容器号 ping redis(连接容器名或者别名)