docker-compose管理容器network與ip問題
更新時間:2024年01月09日 11:09:30 作者:binbreaze
這篇文章主要介紹了docker-compose管理容器network與ip問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
docker-compose管理容器時,通常需要設(shè)定network,指定容器的ip,這樣容器在啟動時會通過指定的ip與宿主機橋接網(wǎng)絡(luò)通信,同樣可以通過ip實現(xiàn)容器之間的通信。
新建docker的network橋接網(wǎng)絡(luò)
docker network create docker1 -o com.docker.network.bridge.name=docker1 -o com.docker.network.bridge.host_binding_ipv4=0.0.0.0 --driver=bridge --subnet=172.20.0.0/16 --gateway=172.20.0.1
- docker1 橋接網(wǎng)卡名稱
- subnet 子網(wǎng)掩碼
- gateway 網(wǎng)關(guān)
查看新建網(wǎng)橋
docker network ls
docker network inspect docker1
docker容器
docker-compose.yml管理多個容器
version: '2' networks: docker1: ### 自建的network external: true services: mgnt: image: mgnt container_name: mgnt hostname: mgnt restart: always command: supervisord -n -c /code/mgnt/supervisord.conf volumes: - /opt/mgnt/conf/serverConfig.json:/code/mgnt/frontend/static/serverConfig.json - /opt/mgnt/conf/oem:/code/mgnt/frontend/static/img/oem - /opt/mgnt/conf/oem.json:/code/mgnt/frontend/static/oem.json - /opt/mgnt/conf/settings.py:/code/mgnt/backend/server/settings.py - /opt/mgnt/conf/supervisord.conf:/code/mgnt/supervisord.conf - /opt/mgnt/conf/redis/redis.conf:/etc/redis.conf - /opt/mgnt/conf/license:/etc/fass/license ports: - "8000:8000" - "80:80" depends_on: - mgnt_mysql - influxdb networks: docker1: ipv4_address: 172.20.0.3 ###容器固定ip mgnt_mysql: image: mgnt_mysql container_name: mgnt_mysql hostname: mgnt_mysql restart: always ports: - 3308:3306 volumes: - /opt/mgnt/data:/var/lib/mysql - /opt/mgnt/conf/mysql/my.cnf:/etc/my.cnf - /opt/mgnt/conf/mysql/conf.d:/etc/mysql/conf.d - /opt/mgnt/conf/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d environment: - MYSQL_PORT=3306 - MYSQL_DATABASE=mgnt - MYSQL_USER=admin - MYSQL_PASSWORD=sysadmin - MYSQL_ROOT_PASSWORD=sysadmin networks: docker1: ipv4_address: 172.20.0.2 influxdb: image: influxdb:alpine container_name: influxdb hostname: influx restart: always volumes: - /var/lib/influxdb:/var/lib/influxdb ports: - "8086:8086" networks: docker1: ipv4_address: 172.20.0.4
開啟容器后docker1網(wǎng)橋內(nèi)容
[ { "Name": "docker1", "Id": "d1f2a4c0afe4b418e855dacc68f7fe3991d93b51c1ebd049ce1c5c81b598ae24", "Created": "2021-02-01T02:41:48.327058853-05:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.20.0.0/16", "Gateway": "172.20.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "53b25b5832b6757f81653e2ed6f5a237d59950825c9e9015272dbd0ff673da5e": { "Name": "mgnt", "EndpointID": "0ed1a8d0d6cf444320e0fe985d2bcfcf70d80111b37928eb1519045f8ffa0336", "MacAddress": "02:42:ac:14:00:03", "IPv4Address": "172.20.0.3/16", ### 容器ip "IPv6Address": "" }, "b3623f6a04d56f7ade8022d289fbbd4fbd3409a5da1c08cd9293511b656481a5": { "Name": "mgnt_mysql", "EndpointID": "f05cf474e861e86e8cd7dfa6a5bdc43368c6af3b01e22d657f92b0a2ace913f1", "MacAddress": "02:42:ac:14:00:02", "IPv4Address": "172.20.0.2/16", ### 容器ip "IPv6Address": "" }, "e99f3d048e384732c12673989aea3814ecbf873e5850d87b36fe70d016861293": { "Name": "influxdb", "EndpointID": "17a3874417ee95b91c421a4beb281df067682eaf46e4f37ab929f76866b6c795", "MacAddress": "02:42:ac:14:00:04", "IPv4Address": "172.20.0.4/16", ### 容器ip "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker1" }, "Labels": {} } ]
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Docker 簡單部署 ElasticSearch的實現(xiàn)方法
這篇文章主要介紹了Docker 簡單部署 ElasticSearch的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09詳解Centos7 下建立 Docker 橋接網(wǎng)絡(luò)
本篇文章主要介紹了詳解Centos7 下建立 Docker 橋接網(wǎng)絡(luò),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Docker安裝Keycloak開源身份認(rèn)證服務(wù)的圖文教程
Keycloak是一款開源的認(rèn)證授權(quán)平臺,本文就來介紹一下Docker安裝Keycloak開源身份認(rèn)證服務(wù)的圖文教程,具有一定的參考價值,感興趣的可以了解一下2024-03-03