docker-compose: using multiple Dockerfiles for multiple services

谁说我不能喝 提交于 2019-12-04 00:11:00

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 :)

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"

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!