1焚鲜、創(chuàng)建一個網(wǎng)絡(luò)
docker network create-d bridge ${netName}
2掌唾、編寫docker-compose.yaml文件,指定網(wǎng)絡(luò)忿磅,例如啟動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}
修改網(wǎng)絡(luò)之后糯彬,一定對容器進行重新構(gòu)建,運行docker-compose down葱她;docker-compose up -d撩扒;
3、編寫需要鏈接mysql81容器的docker-compose.yaml文件吨些,網(wǎng)路指定同一個搓谆,設(shè)置鏈接的容器,代碼如下
不同的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锤灿、測試聯(lián)通
docker exec -it 容器號 ping??redis(連接容器名或者別名)