docker-compose: using multiple Dockerfiles for multiple services

前端 未结 4 1566
陌清茗
陌清茗 2021-02-19 01:37

I\'m using docker-compose and I\'d like to use different Dockerfiles for different services\' build steps. The docs seem to suggest to place different Dockerfiles in different d

相关标签:
4条回答
  • 2021-02-19 02:02

    You have to add it in build section. So, you can specify different alternative dockerfiles for each service.

    services:
      service1:
        build:
            context: .
            args:
                - NODE_ENV=local
            dockerfile: Dockerfile_X
        ports:
            - "8765:8765"
    
    0 讨论(0)
  • 2021-02-19 02:12

    Creator of ShutIt here. Gratified to hear that people are hearing good things about it.

    To be honest, in your position I'd write your own Dockerfile and use standard package management such as apt or yum. A quick check with an ubuntu image and python-pip and python-sqlalchemy are freely available.

    There are more convoluted solutions that may work for you using ShutIt, happy to discuss this offline, as I think it's a bit off-topic. ShutIt was written for this kind of use case, as I could see that this would be a common problem given Dockerfiles' limited utility outside the microservices space.

    0 讨论(0)
  • 2021-02-19 02:21

    This is not possible due to the way Docker handles build contexts.

    You will have to use and place a Dockerfile in each directory that becomes part of the Docker build context for that service.

    See: Dockerfile

    You will in fact require a docker-compose.yml that looks like:

    service1:
        build: service1
    
    service2:
        build: service2
    

    See: docker-compose

    Update:

    To address your particular use-case -- Whilst I understand what you're trying to do and why I personally wouldn't do this myself. The isolation is a good thing and helps to manage expectations and complexity. I would perform the "database creation" as either another container based off your app's source code or within the app container itself.

    Alternatively you could look at more scripted and template driven solutions such as shutit (I have no experience in but heard god thigns about).

    FWIW: Separation of concerns ftw :)

    0 讨论(0)
  • 2021-02-19 02:21

    You can use dockerfile argument in your docker-compose.yml to specify an alternate one for a specific service.

    I don't know when it was added, since the discussion is old, but you can see it's in the reference https://docs.docker.com/compose/compose-file/#dockerfile

    I've tried it yesterday and it works with me. It the base dir for my project I have Dockerfile and Dockerfile-service3 and in the docker-compose.yml:

    version: '2'
    
    services:
        service1:
            build:
                context: .
                args:
                    - NODE_ENV=local
            ports:
                - "8765:8765"
            # other args skipped for clarity
        service2:
            build:
                context: .
                args:
                    - NODE_ENV=local
            ports:
                - "8766:8766"
            # other args skipped for clarity
        service3:
            build:
                context: .
                dockerfile: Dockerfile-service3
                args:
                    - NODE_ENV=local
            ports:
                - "8767:8767"
            # other args skipped for clarity
        service4:
            build:
                context: .
                args:
                    - NODE_ENV=local
            ports:
                - "8768:8768"
            # other args skipped for clarity
    

    In this way all services, except service3 will be built using the standard Dockerfile and service3 will be built using the Dockerfile-service3.

    0 讨论(0)
提交回复
热议问题