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

2

7

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?

Fouquiertinville answered 18/10, 2020 at 5:45 Comment(3)
you need to use Volume if you want to achieve this, more info here - #41935935Corpus
@Corpus I thought, volumes were used mainly across docker containers. First time, I came across usage of volumes in building images.Fouquiertinville
You need to name some directory that contains all the files that you need to copy in in the docker build command (maybe docker build ~/go); the Dockerfile needs to be in that directory (or directory tree, with a -f option) and it can only use relative paths.Senecal
H
9

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.

Havana answered 18/10, 2020 at 6:14 Comment(1)
What you said is correct, I will copy source code and buildFouquiertinville
C
-4

The COPY instruction copies new files or directories from and adds them to the filesystem of the container at the path .

Cuttlefish answered 18/10, 2020 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.