• Uncategorized

About docker : COPY-failed-Forbidden-path-outside-the-build-context

Question Detail

I have a monorepo that has holds various Go services and libraries. The directory structure is like the following:
– Dockerfile

This go.mod file resides in the root of the monorepo directory and the services use the dependencies stated in that file.
I build the Docker image with this command:
docker build -t some:tag ./services/service-a/

When I try to build my Docker image from the root of monorepo directory 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

# Set the Current Working Directory inside the container

# 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?

Question Answer

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.
You can build and manage your docker containers using docker-compose, then this problem can be solved with the help context directive, for example:
├─── src
│ └── folder1
│ └── folder2
│ └── Dockerfile
├── docker-compose.yaml
└── copied_file.ext

version: ‘3’

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

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 [arguments]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.