Let me explain about the app first. I’m using Coolify to manage my apps on my own VM. I have 2 apps, the first one is backend app (containerized) and the second is MariaDB database (containerized).
Previously, whenever I wanted to access my database, I always use public IP address as host. Here is an example of my .env file
DB_HOST="123.123.123.123" #this is public ip address (accessible through internet)
DB_NAME=test
DB_USER=root
DB_PASSWORD=password
DB_PORT=3306
To make it more understandable and easy to read, look at this schema
As you can see, I always access my database through internet. This approach adds more latency, not secure, and if the internet goes down then the app won’t get fetch the data from that database.
Change the IP address in DB_HOST
as container name. Here is the expected schema
The problem now is how do we access the container from the other container? Because, as you can see here, coolify allows this database to accessible from other container (internally in coolify’s docker daemon).
MariaDB URL (internal)
in the right side. You will see container name in the input element, like
thismysql://mariadb:sk7yRRLNfoDfzduhOOfJlAiJswjjMW0@a40k0sgowg0ckw4w448k4gko:3306/default
a40k0sgowg0ckw4w448k4gko
is the container name
services:
app:
build:
context: .
dockerfile: Dockerfile
networks:
- coolify
depends_on: []
networks:
coolify:
external: true
The Dockerfile part is depends on your needs, let’s just focus on networks part in this yml file. In this file, we are trying to access coolify network so that we can access other containers in coolify’s docker daemon
.env
file like thisDB_HOST="a40k0sgowg0ckw4w448k4gko"
DB_NAME=test
DB_USER=root
DB_PASSWORD=password
DB_PORT=3306
After you’ve done setup your docker-compose, Dockerfile and coolify, your app will access database to the database container directly. This way will reduce latency, better security and if the internet is down, the app still can access the database.
Hope this help, thanks for stopping by.