Docker 101: How To Assign Static IP to Docker Containers

Опубликовано: 30 Сентябрь 2024
на канале: WebDev Dave
39,785
488

One of the issues that beginners will encounter is IP assignment to docker containers. By default, IP address are assigned as "first come, first serve". Meaning that depending on the order you start your containers, they can have different IP address.

If you are anything like me, I tend to use multiple environments. While QA would be testing on environment A, I would be developing on environment B. Since I tend not to start up my containers in a specific order, having different IP Address would cause some hiccups because I would need to reconfigure MySQL Workbench to reflect the new IP Address.

So to fix this minor inconvience, I was able to fix this issue by:
doing the following steps:
1) Create a docker network
2) Assign an IP address to the container
3) Enjoy the fruits of my labor

*Command Sample*
docker network create --driver bridge --subnet 172.18.0.0/16 --gateway 172.18.0.1 docker-network

docker network connect --ip 172.18.0.2 docker-network mysql-5.6
docker network connect --ip 172.18.0.3 docker-network mysql-5.7

docker network disconnect docker-network mysql-5.6
docker network disconnect docker-network mysql-5.7