docker-compose: using multiple Dockerfiles for multiple services

前端 未结 4 1555
陌清茗
陌清茗 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: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.

提交回复
热议问题