Here's what I mean. How can I write a docker-compose.yaml file so that when one of the services is "built", it first runs another service?
I'll try to be even more specific. I'm trying to build a Java application using the database library JOOQ, which wants to connect to a database at build time in order to generate Java classes that correspond to database tables. I would like some combination of Dockerfile(s) and docker-compose.yaml file, such that the following happens in roughly the following order.
- A "database" service is started (in my case, using a postgres image).
- The database is initialized with SQL scripts in my development repo.
- The build phase of my "web" service runs, which uses a Dockerfile in the same repo, which invokes the Gradle build, which tells JOOQ to connect to the database service started in step 1. This generates Java files, compiles them, and does everything else for building the container.
- The "web" service is started, connecting to the database service started in step 1.
Here's the docker-compose.yaml file I'm working with
version: '3.7'
services:
postgres:
image: postgres:10.5-alpine
restart: always
ports:
- "6432:5432"
environment:
POSTGRES_DB: flashtools
POSTGRES_USER: flashtools
POSTGRES_PASSWORD: flashtools
volumes:
- ./src/main/scripts/01_init.sql:/docker-entrypoint-
initdb.d/01_init.sql
web:
build: .
network_mode: host
depends_on:
- postgres
ports:
- "8080:8080"
I'll flesh this out with more details, but hopefully what I'm asking is pretty straightforward.
Adepends onB, thenBis ran firstdepends_on. Yes, if a serviceAdepends onB, thenBruns (launches a container) beforeAis run (launches a container), but that says nothing about when Docker builds the image forA. The issue with my compose file is that irrespective ofdepends_onfor running the services, the building_ofAoccurs before the _running ofB. That's a problem if buildingArequires runningBfirst. I suspect there's no way to do this in Docker Compose.