关于为容器创建自定义网络时出现:user specified IP address is supported only when connecting to networks with user configured subnets。错误的解决办法。

docker中,只有使用 --subnet创建的网络才能指定静态IP。因此在使用docker-compose或者docker run命令创建容器时,如果需要指定,则可以使用 :

  • –subnet创建网络(用来指定ip段)
  • –gateway(用来指定网关)
  • my_net3为创建的名字
docker network create --driver bridge --subnet 172.22.1.0/24 --gateway 172.22.1.1 my_net3
#docker-compose.yaml
version: '2'
networks:
  mongocluster_default:
    external: true
services:
  rs_shard_server2:
    image: mongo:3.4
    command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard2-server"
    networks:
     - "mongocluster_default"
    volumes:
      - /database/vol/key.file:/data/mongodb/key.file
      - /database/vol/shard2/config:/etc/mongod
      - /database/vol/shard2/db:/data/db
    ports:
      - "10041:27018"
    restart:
      always
    container_name:
      rs_shard_server2
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

另外,如果想要加入已经存在的网络,若出现以下报错,

Service "rs_shard_server2" uses an undefined network "mongocluster_default"

请确在services同级声明一下外部网络

networks:
  mongocluster_default:
    external: true

eg:

version: '2'
services:
  p1-v3.3:
    image: p1-v3.3:zks
    container_name: p1-v3.3
    hostname: p1-v3.3
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime
      - ./p1:/data/p1
      - ./p1/start.sh:/start.sh
      - /etc/locale.conf:/etc/locale.conf
    privileged: true
    networks:
      home-p1_mynetwork-1:
        ipv4_address: 172.18.0.60
    mac_address: 00:0c:18:cd:ff:60
    ports:
      - 23306:3306
      - 6008:6008
      - 9749:9749
      - 6009:6009
      - 8081:8080
      - 9999:80
    command: ['/bin/sh', -c, '/start.sh']
networks:
  home-p1_mynetwork-1:
     external: true
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐