Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build docker image using different directory contexts

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?

like image 287
luisgepeto Avatar asked Mar 07 '18 21:03

luisgepeto


1 Answers

Use flag -f to set custom path

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

like image 156
trigun117 Avatar answered Oct 05 '22 04:10

trigun117