docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker-compose管理容器network与ip

docker-compose管理容器network与ip问题

作者:binbreaze

这篇文章主要介绍了docker-compose管理容器network与ip问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

docker-compose管理容器时,通常需要设定network,指定容器的ip,这样容器在启动时会通过指定的ip与宿主机桥接网络通信,同样可以通过ip实现容器之间的通信。

新建docker的network桥接网络

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

查看新建网桥

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网桥内容

[
    {
        "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": {}
    }
]

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文