I have a compose file where I have a php
and db
service. In the development environment the application is dependent on the local db
service, but in production there’s already a (non-dockerized) database running. I gave the db
service a profile called "development", but now when I run it in production with docker compose up
without the profile, I get the following:
service db is required by php but is disabled. Can be enabled by profiles [development]
Is there a way to have this depends_on
option only applicable based on a condition? For example if APP_ENV=development
, or something like that? And if not, what would be the most elegant alternative?
version: "3.9"
services:
php:
context: "./docker/php"
target: "${APP_ENV:?}"
depends_on:
db:
condition: "service_started"
db:
build:
context: "./docker/db"
profiles:
- development
2
Answers
The best way is to write startup sh script.
The code I provide bellow is not tested, I wrote as a pseudo code
And I assumed the database you use is Postgres
You can have multiple Compose files. These files are merged together, and override files can have extra settings or entire extra containers.
In the setup you describe, your base Compose file would have the main application, but not the database
You could augment this with settings for production only
or have a separate Compose file with developer-oriented settings. These would include the instructions on how to build the image, and in your setup it would also include the dedicated database container.
Probably the easiest way to use these is to use a symbolic link to make one of the files named
docker-compose.override.yaml
but you can also use a
docker-compose -f
option; note that you need to list every Compose file if you do, and you need to include it on every Compose invocation.