8

My current project consists of a mongo server, a rabbitmq server and a dotnet core service. It is structured as follows:

.
├── project1.docker-compose.yml #multiple docker-compose files for all projects
├── .dockerignore
├── Util/
|   └── some common code across all projects
└── Project1/  #there are multiple projects at the same level with the same structure
    ├── .docker/
    |   ├── mongodb
    |   |   └──Dockerfile
    |   └── rabbitmq
    |       └──Dockerfile
    ├── BusinessLogicClasses/
    |   └── some classes that contain my business logic 
    └── DotNetCoreService/
        ├── my service code 
        └── .docker
            └──Dockerfile

Right now I am able to use docker-compose command to build the images for mongodb, rabbitmq and the dot net core succesfully. The docker-compose.yml sits at the home directory level because my different projects (in this case Project1) references code found under the Util directory. Therefore I need to be able to provide a context that is above both directories so that I can use COPY operations on the Dockerfile.

My basic project1.docker-compose.yml is as follows (I excluded not important parts)

version: '3'
services:
  rabbitmq:
    build: 
      context: Project1/.docker/rabbitmq/

  mongodb:    
    build:
       context: Project1/.docker/mongodb/

  dotnetcoreservice: 
    build:
      context: ./
      dockerfile: Project1/DotNetCoreService/.docker/Dockerfile

As can be seen, the context for the dotnetcoreservice is at the home directory level. Therefore my Dockerfile for that specific image needs to target the full paths from the context as follows:

#escape=`
FROM microsoft/dotnet:2.0-sdk AS build
WORKDIR /app
COPY Project1/ ./Project1/
COPY Util/ ./Util/
RUN dotnet build Project1/DotNetCoreService/

This dockerfile works succesfully when invoked via the docker-compose command at the home directory level, however when invoked via the docker build .\Project1\DotNetCoreService\.docker\ command it fails with the following message:

COPY failed: stat /var/lib/docker/tmp/docker-builder241915396/Project1: no such file or directory

I think this is a matter of the actual context because the docker build instruction automatically sets the context to where the Dockerfile is. I would like to be able to use this same directory structure to create images both with the docker-compose build as well as with the docker build instructions.

Is this somehow possible?

2
  • 1
    try to add custom path using -f Commented Mar 7, 2018 at 21:56
  • You are completely right. I ended up using docker build -f .\Project1\DotNetCoreService\.docker\Dockerfile . Commented Mar 8, 2018 at 14:40

2 Answers 2

13

Use flag -f to set custom path

Example: docker build --rm -t my-app -f path/to/dockerfile .

Sign up to request clarification or add additional context in comments.

2 Comments

what if the dockerfile I want to use is not a subdir of the current folder?
@wawawa then update the context path which is the dot at the end of that command and then update the -f path along with it
3

May 2022: The new releases of Dockerfile 1.4 and Buildx v0.8+ come with the ability to define multiple build contexts.

This means you can use files from different local directories as part of your build.

Dockerfiles now Support Multiple Build Contexts

Tõnis Tiigi

Multiple Projects

Probably the most requested use case for named contexts capability is the possibility to use multiple local source directories.

If your project contains multiple components that need to be built together, it’s sometimes tricky to load them with a single build context where everything needs to be contained in one directory.

There’s a variety of issues:

  • every component needs to be accessed by their full path,
  • you can only have one .dockerignore file,
  • or maybe you’d like each component to have its own Dockerfile.

If your project has the following layout:

project
├── app1
│   ├── .dockerignore
│   ├── src
├── app2
│   ├── .dockerignore
│   ├── src
├── Dockerfile

…with this Dockerfile:

#syntax=docker/dockerfile:1.4
FROM … AS build1
COPY –from=app1 . /src

FROM … AS build2
COPY –from=app2 . /src

FROM …
COPY –from=build1 /out/app1 /bin/
COPY –from=build2 /out/app2 /bin/

…you can invoke your build with docker buildx build –build-context app1=app1/src –build-context app2=app2/src .. Both of the source directories are exposed separately to the Dockerfile and can be accessed by their respective names.

This also allows you to access files that are outside of your main project’s source code.

Normally when you’re inside the Dockerfile, you’re not allowed to access files outside of your build context by using the ../ parent selector for security reasons.

But as all build contexts are passed directly from the client, you’re now able to use --build-context othersource=../../path/to/other/project to avoid this limitation.


May 2023: issue 37129 "add support for multiple (named) build-contexts" reports it is now supported with:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.