74

I have used docker to create CLI interfaces where I test my code. These are named reasonably as:

proj_root/.../docks/foo.dockerfile
proj_root/.../docks/bar.dockerfile

Because there is more than one dock involved, the top level "Dockerfile" at the project root is unreasonable. Although I can't copy ancestor directories when building in docker, I can clone my entire repo.

So my project architecture works for me.


Next, I look up docker-compose because I need to match my docker cards up against a postgres db and expose some ports.

However, docker-compose seems to be anchored to the hard-coded '"Dockerfile" in the current working directory' user concept from the perspective of the command line interface.

But! I see the error message implies the tool is capable of looking for an arbitrarily named dockerfile:

ERROR: Cannot locate specified Dockerfile: Dockerfile

The question is: how do I set docker-compose off looking for foo.dockerfile rather than ./Dockerfile?

Chris
  • 28,822
  • 27
  • 83
  • 158

2 Answers2

129

In your docker-compose, under the service:

services:
  serviceA:
    build: 
      context: <folder of your project>
      dockerfile: <path and name to your Dockerfile>
pb11pb
  • 148
  • 1
  • 6
Mihai
  • 9,526
  • 2
  • 18
  • 40
  • 4
    This is not what the user is looking for. Ideally there would be a way to build a specific Dockerfile, like `docker-compose build -i Dockerfile.staging` instead of having multiple docker-compose files. – Marc LaBelle Dec 23 '20 at 14:47
  • 1
    What I ended up doing was using ARGs for anything that needed to be changed during the build process and using just one Dockerfile. May not work for every situation, but it works for CI/CD branches. – Marc LaBelle Dec 23 '20 at 14:53
60

As mentioned in the documentation of docker-compose.yml, you can overwrite the Dockerfile filename within the build properties of your docker-compose services.

For example:

version: 3
services:
  foo:
    image: user/foo
    build:
      context: .../docks
      dockerfile: foo.Dockerfile
  bar:
    image: user/bar
    build:
      context: .../docks
      dockerfile: bar.Dockerfile
ErikMD
  • 13,377
  • 3
  • 35
  • 71