简体   繁体   中英

How to copy a file from host machine to Docker container using COPY directive in docker image?

I am trying to build a docker image that will copy my cpp file from my host machine to the docker container, compile it and finally I will run it. I am facing a problem where I am unable to copy from source file cpp file from host to the docker container. I checked the docker document and it says I should use -f- option but when I do that the docker build just hangs. Here is my docker file

FROM debian:stretch-slim as base

RUN apt-get update && apt-get install -y --no-install-recommends \
    sudo \
    vim \
    nano \
    tree \
    git \
    locales \
    locales-all \
    ssh \
    python3 \
    less \
    net-tools \
    iputils-ping \
    gcc  \
    g++  \
    meson \
    ninja-build 

WORKDIR /root

COPY docker_test.cpp ./
COPY meson.build ./
RUN meson builddir
RUN cd builddir
RUN ninja docker_test 

ENTRYPOINT ["/bin/bash", "-c"]

CMD ["./docker_test"]

I am trying to build this docker file. My docker_test.cpp lives inside my host machine and it is in the same folder

Error I get is

    docker build  ./Docker/

    Sending build context to Docker daemon  2.048kB
    Step 1/10 : FROM debian:stretch-slim as base
     ---> da2dadc5e951
    Step 2/10 : RUN apt-get update && apt-get install -y --no-install-recommends     sudo     vim     nano     tree     git     locales     locales-all     ssh     python3     less     net-tools     iputils-ping     gcc      g++      meson     ninja-build
     ---> Using cache
     ---> c4985c6dd624
    Step 3/10 : WORKDIR /root
     ---> Running in b5e340b626e3
    Removing intermediate container b5e340b626e3
     ---> eb8ba73448a3
    Step 4/10 : COPY docker_test.cpp ./
    COPY failed: stat /var/lib/docker/tmp/docker-builder742117699/docker_test.cpp: no such file or directory

My folder structure -

     ls -l
    total 16
    drwxrwxr-x 2 sh sh 4096 Apr 18 13:30 Docker
    -rwxrwxr-x 1 sh sh  197 Apr 18 12:58 docker_test.cpp
    -rwxrwxr-x 1 shr sh   69 Apr 18 12:58 meson.build
    -rwxrwxr-x 1 sh sh  399 Apr 18 12:58 README.txt

The Dockerfile lives inside the folder Docker.

The "directory" parameter to docker build specifies a context directory . The contents of this directory are sent to the Docker daemon, and the Dockerfile can't access anything outside of this directory tree.

In particular when you specify

docker build  ./Docker/

then all COPY paths are interpreted relative to that Docker subdirectory, and you won't have access to the root directory containing your source file.

This means the "path" argument to docker build must be the root of your source tree, usually . . If you don't want to rearrange your source tree then you can use the docker build -f option to specify an alternate Dockerfile

docker build -f ./Docker/Dockerfile .

It's very common to just put a Dockerfile (and, if appropriate, docker-compose.yml ) in the repository root directory and not try to put them somewhere else, which avoids this problem entirely.

You should make sure that Dockerfile & whatever the files that you want to copy into the container should be in the same folder then try to build the Dockerfile again.It will build the docker image successfully.

Docker allows the 'dot dot' syntax which will copy the entire contents of you $CWD on your local machine into the Dockerfile at whatever WORKDIR you're currently in. You can create a .dockerignore file that allows you to specify files you want to omit when doing this copy process.

.dockerignore

Docker*
*.txt

Dockerfile

FROM debian:stretch-slim as base

RUN apt-get update && apt-get install -y --no-install-recommends \
    sudo \
    ...
    ninja-build 

WORKDIR /root

COPY . .

# do the rest of your build
# you will have /root/docker_test.cpp & /root/meson.build inside the image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM