For now in docker-compose.yml
I have grouped services by naming them like api
, api-mysql
, blog
, blog-mysql
, blog-redis
, frontend
, frontend-something
etc. Then main services (api, blog) has depends_on
with listed needed services. And it works fine.
But while file grows, it becomes less and less readable (because it’s long). I’m wondering if there is better way to group services for such scenario? Can I for example have docker-compose.yml
with global services like phpMyAdmin and inside include specific ones docker-compose-api.yml
and docker-compose-blog.yml
?
I cannot find if is there a better (more readable)
3
Answers
You can use Docker multi-stage. You need to create unique Dockerfiles for separated services, here the image means: Docker build the image with this name
After that you can build the environment with docker-compose up -d –build command. This command need to run only one (except when build failed) after the build successfully finished you can use your services with general commands docker-compose start/stop/restart
Looks like there is no way to include
docker-compose
file into other one.BUT if you want to extend some of your services from other file, welcome…
As
Adam P.
mentioned, the only way to do it more clean is to make complex command likedocker-compose -f docker-compose-core.yml -f docker-compose-api.prod.yml -f docker-compose-frontend.prod.yml up -d
.PS: Actually, hate it a bit. Now I have to create a script that just runs this large command.
You could invoke docker compose with multiple -f files. I don’t know that it’d be more readable, but you could…
docker compose -f docker-compose.yml -f docker-compose.database.yml -f docker-compose.microservices.yml -f docker-compose.test-harness.yml ...
etc.
We always use Makefiles with docker so we end up with things like:
make app
# launch the app with the databases so you can do basic stuffmake app_with_services
# launch the app with the databases and servicesmake test
# launch app with everything including the test harness and run the tests