1

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:9090 -v /home/eugene/dev/shared:/opt/shared -d eugene/dev_img_1.3

I'm getting the following error:

Error response from daemon: Cannot start container b2242e5da6e1b701ba4880f25fa8d465d5f008787b49898ad9e46eb26e417e48: port has already been allocated

I really do not care about port 10000. My goal is to have a specific container IP of my choosing, as well as to have ports 9000 and 9090 exposed to the host.

I have looked at some other questions, but did not see a clear syntax to do this

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167

3 Answers3

1

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.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • In my container I run /usr/sbin/sshd among other things, and I need to know from the host - what IP I can ssh to – Eugene Goldberg Feb 06 '15 at 16:49
  • Could you just run `docker inspect -f "{{.NetworkSettings.IPAddress}}" CONTAINER` to find the IP? Also, if possible, prefer `docker exec` to ssh. – Adrian Mouat Feb 06 '15 at 17:06
  • I guess I would settle for mapping container's port 22 to some higher port on the host. would that be in a form of -p 22:22222 or the other way around? – Eugene Goldberg Feb 06 '15 at 17:14
  • Other way around: `-p HOST_PORT:CONTAINER_PORT`. Note you can also use `-P` which will automatically assign a high numbered port for each exposed container port, which is handy if you have several containers with similar services. – Adrian Mouat Feb 06 '15 at 17:27
1

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

Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75
  • 1
    --ip is not recognized by docker – Eugene Goldberg Feb 06 '15 at 16:00
  • Can you tell me how you are using it ?? Please look at how to use it properly. Here are some of the links from the official docker website https://docs.docker.com/articles/networking/, https://docs.docker.com/articles/networking/#binding-ports – Tejus Prasad Feb 06 '15 at 16:03
  • here is how I'm using it: sudo docker run -it -p 9000:9000 -p 9090:9090 --ip=172.17.0.2 -v /home/eugene/shared:/opt/shared -d eugenegoldberg/dev_img_1.2 – Eugene Goldberg Feb 06 '15 at 16:12
  • I have edited the answer as per your requirements. I guess the 2nd part of the answer is most suitable for your requirements. – Tejus Prasad Feb 06 '15 at 16:44
  • I ran: sudo docker run -it -p 9000:9000 -p 9090:9090 --fixed-cidr=172.17.0.2/24 -v /home/eugene/dev/shared:/opt/shared -d eugenegoldberg/dev_img_1.2 - and got: flag provided but not defined: --fixed-cidr – Eugene Goldberg Feb 06 '15 at 16:48
  • This is a wrong way of using it. They are not flags. You need to edit it in the specified location. Please go through the above links i mentioned. Try to understand how its used as mentioned in the documentation. – Tejus Prasad Feb 06 '15 at 16:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70406/discussion-between-programminggeek-and-eugene-goldberg). – Tejus Prasad Feb 06 '15 at 16:54
  • `--ip` sets the IP address of the _host_ that is used for the bound port. My understanding is that the OP wants to set the IP address of the _container_. (@programmingGeek, I think you understand this, but I'm not sure it will be clear to others). – Adrian Mouat Feb 06 '15 at 17:48
1

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.

user2105103
  • 11,599
  • 2
  • 18
  • 11