Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile - How to copy files from a local folder? [duplicate]

Below is the Dockerfile:

FROM golang:1.14.10
MAINTAINER xyz

COPY ~/go/bin/product-api /go/bin/product-api

COPY ~/go/bin/swagger /go/bin/swagger

ENTRYPOINT ["/go/bin/product-api"]

on docker build -t cloud-native-product-api:1.0.0 ., gives error:

Step 3/5 : COPY ~/go/bin/product-api /go/bin/product-api
COPY failed: stat /var/lib/docker/tmp/docker-builder398080099/~/go/bin/product-api: no such file or directory

I think the image build process takes /var/lib/docker/tmp/docker-builder398080099 as workspace and refer from that path.

How to copy files from specific folder of local machine into Docker image?

like image 461
overexchange Avatar asked Oct 18 '20 05:10

overexchange


People also ask

Does Docker copy overwrite existing file?

It seems that docker build won't overwrite a file it has previously copied. I have a dockerfile with several copy instructions, and files touched in earlier COPY directives don't get overwritten by later ones. After building this, $BASE/config/thatfile. yml contains the contents of file1.

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.


1 Answers

The key thing you are missing is the build's context, relevant from the COPY part of the docs:

The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

https://docs.docker.com/engine/reference/builder/#copy

Description of "context" here.

https://docs.docker.com/engine/reference/commandline/build/

But essentially, when you say "docker build directory-with-docker-file" COPY only sees files in (and below) the directory with the Dockerfile.

What you probably want to do is compile "swagger" during the docker build, and then put it in the path you want.

A key thing to remember is that a Dockerfile is generally meant to be reproducible such that if you run docker build on ten different hosts, the exact same image will be produced. Copying files from arbitrary locations on the host wouldn't lead to that.

like image 67
Matt Blaha Avatar answered Oct 17 '22 23:10

Matt Blaha