Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container with DHCP assigned address

I have a server with several virtual machines running. I am starting a container with Jira installation and i need the container to be assigned with different address from the DHCP and not use the host IP address. I am a noobie so please explain

like image 527
FixXxeR Avatar asked Aug 16 '18 08:08

FixXxeR


2 Answers

The technique suggested in @ad22's answer requires a custom build of the Docker engine that uses a fork of libnetwork. Now, more than four years after that hack was developed, the DHCP feature has still not been merged into the standard Docker engine, and the fork has fallen far behind the mainline code.

Since late 2019, it has been possible to assign IP addresses to Docker containers with DHCP using devplayer0's docker-net-dhcp plugin, which works with the standard Docker engine. When you create a new container, this plugin starts a Busybox udhcpc client to obtain a DHCP lease, then runs udhcpc (in a process outside the container's PID namespace) to renew the lease as needed.

like image 183
200_success Avatar answered Sep 24 '22 03:09

200_success


As found in the other answer, using the macvlan will not enable the container to obtain addresses from DHCP. The functionality to obtain addresses from DHCP is experimental (this was created by someone associated with the docker libnetwork project)

https://gist.github.com/nerdalert/3d2b891d41e0fa8d688c

It suggests compiling the changes into the docker binary and then running

docker network create -d macvlan \
  --ipam-driver=dhcp \
  -o parent=eth0 \
  --ipam-opt dhcp_interface=eth0 mcv0

Since this requires re-compiling the binary, an alternate solution could be to assign static IP addresses to all your containers using the "--ip" option to docker run/compose, and get a DNS entry for your hostname assigned to this IP, and also ensure that the IP can never be assigned through DHCP.

like image 40
ad22 Avatar answered Sep 21 '22 03:09

ad22