how to properly specify an IP for a docker container

后端 未结 3 1944
天命终不由人
天命终不由人 2021-01-25 20:25

I\'m trying to explicitly specify an IP address for my docker container in the following way:

sudo docker run -it -p 172.17.0.2:10000:10000 -p 9000:9000 -p 9090:         


        
相关标签:
3条回答
  • 2021-01-25 20:38

    The -p argument is used to forward ports from the container to the host, not for assigning IPs.

    There is no easy way to assign a fixed IP to a Docker container and I would strongly advise you not to try. Instead re-architect your system so that it isn't dependent on a fixed IP. If this really isn't possible, I think you can choose an IP by using the LXC execution driver and various flags, but I would strongly recommend against this.

    0 讨论(0)
  • 2021-01-25 20:45

    You can assign a fixed ip using pipework, but it's not "the docker way". I would agree with Adrian. Re-design away from fixed IP's.

    0 讨论(0)
  • 2021-01-25 20:50

    This can be done in different ways.

    You can edit your system-wide Docker server settings (by editing DOCKER_OPTS in /etc/default/docker) and add the option --ip=IP_ADDRESS in Ubuntu and then restart your server. If you are using only 1 docker container and want to have dockers IP same as your host, start the docker container using --net=host flag to set the container to have the host machine IP address.

    Other way is to have these options configured at server startup(by editing DOCKER_OPTS in /etc/default/docker): --bip=CIDR — to supply a specific IP address and netmask for the "docker0" bridge, using standard notation like 192.168.1.8/23. For example with --fixed-cidr=192.168.1.0/25, IPs for your containers will be chosen from the first half of 192.168.1.0/24 subnet. The "docker0" Ethernet bridge settings are used every time you create a new container. You are trying to bind a container's ports to a specific port using the -p flag , which will not help you in assigning a IP address to the container.

    Another way to assign a IP address in any particular range(Example: 172.30.1.21/30). Stop the docker using stop docker , then use ip link and ip addr commands to set up the "bridge br0" and start docker using docker -d -b br0

    0 讨论(0)
提交回复
热议问题