16

My containers should share the same source code folder.

But they should build from different Dockerfiles.

Is it possible to build them with docker-compose and build from different Dockerfiles at a time?

I wish there is a syntax like

build: . -f <<Dockerfile_ABC> build: . -f <<Dockerfile_CDE>

abc:
  build: . -f <<Dockerfile_ABC>
  environment:
    - PROJECT=abc
  command: ruby abc/abc.rb
  volumes:
      - ./:/working_direcotry
  ports:
    - 5904:5904

cde:
  build: . -f <Dockerfile_CDE>
  environment:
    - PROJECT=cde
  command: ruby cde/cde.rb
  volumes:
      - ./:/working_direcotry
  ports:
    - 5902:5902
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • you should be fine to raise pull request to docker github directly for your idea – BMW Jun 13 '15 at 07:59
  • Is this a possible repeat of http://stackoverflow.com/questions/29835905/docker-compose-using-multiple-dockerfiles-for-multiple-services. At first I misunderstood this question (ignored the docker-compose part) and wrote an answer. I deleted that answer now, and posted this comment instead haha. – Gobi Dasu Jan 06 '17 at 07:52

2 Answers2

26

The following worked for me

abc:
  build:
    context: .
    dockerfile: Dockerfile.one
def:
  build:
    context: .
    dockerfile: Dockerfile.two

Of course you can tweak the context as needed if you have different contexts. I also use this to have separate Dockerfile.production versions to set things up differently for production versions of the app.

Tyrone Wilson
  • 4,328
  • 2
  • 31
  • 35
-1

Just specify the name of the Dockerfile:

abc:
    build: path/to/Dockerfile_for_abc
OrangeTux
  • 11,142
  • 7
  • 48
  • 73
  • 2
    My guess that the author want the same build context for all services. Once the Dockerfile is in the a different path, the build context changes to that path as well. – Torsten Engelbrecht Jun 15 '15 at 14:20
  • Answer is confusing about what are you specifying, it's not a 'name of the Dockerfile' its a completely different folder, specifying this as full path to another docerfile will lead to an error: `failed to read dockerfile: error from sender: ..../Dockerfile_for_abc: not a directory` – Алексей Лещук Jan 15 '22 at 12:09