I have a project sample with two services, database and app, declared in docker-compose.yml:
version: "3.8"
services:
database:
image: sample/database
build:
context: .
dockerfile: database.Dockerfile
network: sample_default
ports:
- "8001:5432"
app:
image: sample/app
build:
context: .
dockerfile: app.Dockerfile
network: sample_default
args:
- DATABASE_URL=postgresql://postgres:password@database:5432/cluster
depends_on:
- database
networks:
sample_default: {}
database.Dockerfile takes care of downloading and installing Postgre, as well as starting a Postgre cluster and database within that cluster. The tables of this database are created through CREATE TABLE at build time under a RUN command.
app.Dockerfile takes care of downloading and installing the compiler, as well as compiling the source code.
In order for app.Dockerfile's image to be built, database.Dockerfile's image must be built first. This is because, at compile time, SQL query strings within the source code are validated against the database created in database.Dockerfile.
The compiler uses DATABASE_URL to connect to the Postgre database which contains the tables to validate the queries against. Within this DATABASE_URL, the specified address is database:5432, since service discovery can be used on non-default bridge networks.
My problem is that in running docker-compose up, app at build time cannot connect to database at build time. A RUN ping database in app.Dockerfile fails with ping: database: No address associated with hostname. Yet, starting a container from both images on the sample_default network (manually instead of through docker-compose) and running ping database from app's container is successful.
I have already specified the network under build in docker-compose.yml, so what can I do on top of that to allow build-time networking here?