skip to Main Content

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


  1. 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

    version: '3.8'
    
    services:
      api:
        tty: true
        build:
          context: .
          dockerfile: Dockerfile.api
          target: api
        image: api:latest
        ...
        ...
    
      blog:
        tty: true
        build:
          context: .
          dockerfile: Dockerfile.blog
          target: blog
        image: blog:latest
        ...
        ...
    
      frontend:
        tty: true
        build:
          context: .
          dockerfile: Dockerfile.frontend
          target: frontend
        image: frontend:latest
    
      api-mysql:
        tty: true
        build:
          context: .
          dockerfile: Dockerfile.api-mysql
          target: api-mysql
        image: api-mysql:latest
        ...
        ...
        depends_on:
          - api
      ..
      ...
      ...
      etc...
    
    Login or Signup to reply.
  2. 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 like docker-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.

    Login or Signup to reply.
  3. 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 ...

    docker-compose.yml:
    # Just the app!
    services:
      app:
        ...
    
    docker-compose.database.yml:
    # Database things
    services:
      app:
        depends_on:
          - some_database
          - elasticsearch
          - memcached
    
      some_database:
        ...
    
      elasticsearch:
        ...
    
      memcached:
        ...
    ...
    

    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 stuff
    • make app_with_services # launch the app with the databases and services
    • make test # launch app with everything including the test harness and run the tests
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search