Static ip address to connect different containers (MSSQL, CHATBOT, LETSENCRYPT)

We have setup three docker containers with docker-compose that run a MSSQL DB, a chatbot and a letsencrypt/NGINX container. It all works fairly well. The issue is, that if we re-run the docker-compose file that each of the three containers gets a different IP address.
Though - in each container we have config files that contain the ip address of one of the other containers - for example the chatbot container has a config file with the ip adress of the mssql database.
We are a little unclear what the best approach is to overcome this issue. We have read, that static ip addresses are not the best way in docker? What would be the best approach?

Thank you.

You can use the service name.

version: "3"
services:
 mssql:
   ...

 chatbot:
   ...
 
 letsencrypt:
   ...

These services can resolve eachother by these names, meaning from the chatbot container, you should be able to ping “mssql”, so you dont have to think about ips

2 Likes
version: "3.2"
services:
  apache:
    image: ...
    networks:
      backend:
        ipv4_address: 172.20.1.5
  ...
  mysql:    
    networks:
      backend:
        ipv4_address: 172.20.1.2
networks:
  frontend:
  backend:
    ipam:
      config:
        - subnet: 172.20.1.0/24

And there you got two containers with fixed IP.

1 Like

Docker uses service discovery not need to have fixed IP

1 Like

Thx for those hints - issue could be resolved!

It is funny to observe that docker beginners try to enforce old habits to the container world. With limited or no knowledge at all they draft a solution and try to forcefully apply it, no matter what…

At the same time subject matter experts like terpz provide the solution that actualy is needed… and it gets ignored because it is not the solution they understand… pitty.

At the end everyone is free to make as many mistakes as they want… No one can force you to do it right. Though, life is easier if you understand your tooling and use things how they are intended to be used…

2 Likes

i have created a stopped application using docker compose with a MySQL dependency that i used docker container to create but now what happens each time i closed stopped the application i have to change the MySQL host Ip in laravel container.