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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…