This tutorial will be short and precise. I have tried to make it more simple by providing you the exact compose file.
There comes a scenario once in a while where when we dockerize our application or any kind of service they should be interacting internally with containers.
And at that moment the simplest way to achieve this is by creating a docker network.
Creating a docker network will enable you to assign static IPs to your individual services mentioned in the compose file. For instance MySQL, Redis, kafka, etc.
To list the existing network, you can use the below command.
docker network ls
One of the advantages of Docker containers and services is that they can be connected to each other or to non-Docker workloads.
Docker containers and services don't have to be conscious that they're running on Docker, or whether or not their peers are Docker workloads.
version: '3'
networks:
mynetwork:
ipam:
config:
- subnet: 172.25.0.0/24
services:
db:
image: mysql
environment:MYSQL_ROOT_PASSWORD: root
MYSQL_DB: db
MYSQL_USER: user
ports:
- 3306:3306
networks:
mynetwork:
ipv4_address: 172.25.0.5In this, we have assigned a static IP to service and mentioned a subnet above. This will allow docker containers to communicate seamlessly with each other.
Now to check the container IP address you can fire the below command.
docker inspect "container_id" | grep IPAdAs you can see the above compose file has MySQL service mentioned, in a similar manner you can change it accordingly to your choice or need.
And yes if you are just copying the above compose file please be aware it might give an indentation error so just correct it if it occurs.
Thanks, will be putting out some more content soon.
Also;
👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.
Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬