Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COPY failed: Forbidden path outside the build context

Tags:

docker

go

I have a monorepo that has holds various Go services and libraries. It has a structure of

monorepo
 services 
  service-a 
   - Dockerfile
go.mod
go.sum

My go.mod resides in the root of the monorepo and the services use the dependencies stated in the go.mod file.

I build the Docker image with

docker build -t some:tag ./services/service-a/

When I try to build my Docker image from the root of the monorepo with the above docker command I get the following error.

COPY failed: Forbidden path outside the build context: ../../go.mod ()

Below is my Dockerfile

FROM golang:1.14.1-alpine3.11

RUN apk add --no-cache ca-certificates git

# Enable Go Modules
ENV GO111MODULE=on

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY ../../go.mod go.sum ./

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o service-a

ENTRYPOINT ["/app/service-a"]

Is there something I have to do to be able to add files into my Docker image that aren't in the current directory without having to have a separate go.mod and go.sum in each service within the monorepo?

like image 927
pocockn Avatar asked Apr 13 '20 20:04

pocockn


People also ask

How do I put files outside docker's build context?

The best way to work around this is to specify the Dockerfile independently of the build context, using -f. For instance, this command will give the ADD command access to anything in your current directory. docker build -f docker-files/Dockerfile . docker build -f ../Dockerfile .

What is build context docker?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

Can Dockerfile copy from parent directory?

It turns out that you cannot include files outside Docker's build context. However, you can copy files from the Dockerfile's parent directory.


2 Answers

Docker only allows adding files to the image from the context, which is by default the directory containing the Dockerfile. You can specify a different context when you build, but again, it won't let you include files outside that context:

docker build -f ./services/service-a/Dockerfile .

This should use the current directory as the context.

Alternatively, you can create a temp directory, copy all the artifacts there and use that as the build context. This can be automated by a makefile or build script.

like image 96
Burak Serdar Avatar answered Sep 28 '22 09:09

Burak Serdar


You can build and manage your docker containers using docker-compose, then this problem can be solved with the help context directive, for example:

project_folder  
├─── src  
│    └── folder1
│        └── folder2
│            └── Dockerfile
├── docker-compose.yaml   
└── copied_file.ext 

docker-compose.yaml

version: '3'
services:

    your_service_name:
        build:
            context: ./ #project_folder for this case
            dockerfile: ./src/folder1/folder2/Dockefile

Dockerfile

FROM xxx
COPY copied_file.ext /target_folder/

build or rebuild services:

docker-compose build

run a one-off command on a service:

docker-compose run your_service_name <command> [arguments]
like image 40
amlagoda Avatar answered Sep 28 '22 10:09

amlagoda