Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

How do docker containers or services find each other specifically in a particular network?

I have a web(frontend and backend) application and a database running as services under the same network. How would the backend service or the container specifically find out the database service(container) and communicate with that?? How would the backend service particularly have the database service's IP (let's say the DNS name or the container name) as the database endpoint in it's application when there is frontend service too in the network???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The database connection code on the backend can be written by use of environmental variables instead of hardcode them(Example: process.env.POSTGRES_PASSWORD). While creating an Dockerfile for the application we can define ENV variables within it and these can be even overridden during the startup of the container. If one is using docker compose, we can define these database variables either in a .env file which can be used in the docker-compose.yml or we can directly define the environmental variables inside the docker-compose.yml. I suggest one to have a .env file because that usually acts a bridge between our source code and docker-compose file and by changing the variables there we can make sure our container runs fine without looking at the compose file or source code. Therefore, the docker container like a backend container finds a database container.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...