119

I want to create a docker image. This is my work directory: Dockerfile.in test.json test.py

And this is my Dockerfile:

COPY ./test.json /home/test.json
COPY ./test.py /home/test.py

RUN python test.py

When i launch this command: docker build -f Dockerfile.in -t 637268723/test:1.0 .

It gives me this error:

`Step 1/5 : COPY ./test.json /home/test.json
 ---> Using cache
 ---> 6774cd225d60
 Step 2/5 : COPY ./test.py /home/test.py
 COPY failed: stat /var/lib/docker/tmp/docker-builder428014112/test.py: 
 no such file or directory`

Can anyone help me?

EdoBen
  • 1,676
  • 2
  • 14
  • 24
  • Is that from some kind of build system? Or where does the path come from? – Klaus D. Oct 24 '17 at 10:56
  • 15
    Is test.py or *.py in your dockerignore? – Jay Dorsey Oct 24 '17 at 12:16
  • 4
    Is test.py available in current directory where Docker filed exitsts. – Jinna Balu Oct 24 '17 at 12:36
  • @KlausD. that path looks like what I see docker using internally for building images (e.g., not a build system, it's just docker output when there is a file failure – Jay Dorsey Oct 24 '17 at 13:16
  • 6
    Did you ever solve this? Having the same problem. – emmdee Nov 22 '17 at 18:39
  • 4
    Docker still presents buggy behaviors on it's engine, imho. I used the same `COPY` statement on my Dockerfile, and had the same issues. Tried `COPY /host_file /container_folder` (without dot), and it worked. After this, tried the former `COPY` that you have here, and it worked normally (wtf?)! One thing that I made different, was to remove all images (cache) that Docker generates on the build process. My guess, is that trash might be still stored on these intermediary images (I'm using Docker 18.09.5). Take a look on SO or GitHub, SO MANY issues about 'copying host -> container. Bad omen. – ivanleoncz Apr 28 '19 at 19:26
  • 1
    Wow man, ivanleoncz thank you I resolved this problem. I had this clause: ADD mcint_swagger_hub-1.0.0.jar /opt/jboss/wildfly/standalone/deployments/mcint_swagger_hub_api-1.0.0.jar and I put a simple slash before the mcint_swagger_hub-1.0.0.jar , so now i got this: ADD /mcint_swagger_hub-1.0.0.jar /opt/jboss/wildfly/standalone/deployments/mcint_swagger_hub_api-1.0.0.jar and I have no problem. Thanks bro!! God Bless you – Richard Rebeco Feb 24 '20 at 18:12
  • in my case, it's just because of non-existing file like ./test.py in the folder of Dockerfile – joe-khoa Oct 30 '20 at 07:56
  • For me, on Windows, COPY seems to fail if a `.dockerignore` file even *exists* and works when I delete .dockerignore, irrespective of the contents of .dockerignore – Chris F Carroll Mar 05 '21 at 21:02
  • This could also happen if you're ignoring all files in .dockerignore using wildcard `*` but not including specific files you want to be COPYed like his: `!file_to_copy.ext` – Taoufik Mohdit Dec 14 '21 at 14:18

22 Answers22

62

You should put those files into the same directory with Dockerfile.

Ivan Lee
  • 3,420
  • 4
  • 30
  • 45
  • Because I was using 'from python:2.7-onbuild' it copied all the sibling and child files from Dockerfile location anyway. So, I finally removed the COPY lines from Dockerfile and the files being as sibling files were copied too. – Kuba Nov 28 '18 at 00:41
  • 4
    If this doesn't work, check your `.dockerignore` file! – Kenny Evitt Aug 06 '20 at 21:30
  • My `script` part of my `gitlab-ci.yml` file was: `- cd cmd/ZpmHttpServer/ - apk add git - go get - go build -o ../../ZPMOddajaNalog_API`. The following part was missing: `../../` which was the reason that the Dockerfile was not in the same directory. – tedi Mar 04 '21 at 22:07
53

Check if there's a .dockerignore file, if so, add:

!mydir/test.json
!mydir/test.py
tete
  • 370
  • 3
  • 13
  • 2
    This one worked for me, the whole folder was ignored, then docker failed to create the image giving a bad impression driving me mad about the file paths. – Marco Medrano Jun 25 '19 at 19:59
  • This worked for me as well. In my case the folder I was referring to was in .dockerignore, so removed from it. – Durga P. Kapa Oct 03 '20 at 23:04
18
  1. Q1: Check your .dockerignore file in build path, the files or dir you want to copy may be in the ignore file list!
  2. Q2: The COPY directive is based on the context in which you are building the image, so be aware of any problems with the directory where you are currently building the image! See: https://docs.docker.com/engine/reference/builder/#copy
lupguo
  • 1,525
  • 13
  • 13
9

I had to use the following command to start the build:

docker build .

tedi
  • 6,350
  • 5
  • 52
  • 67
8

Removing ./ from source path should resolve your issue:

 COPY test.json /home/test.json
 COPY test.py /home/test.py
8

Make sure the context you build your image with is set correctly. You can set the context when building as an argument. Example: docker build -f ./Dockerfile .. where '..' is the context in this example.

Kevin
  • 2,760
  • 3
  • 15
  • 30
  • 2
    THANKS!!! this helped me a lot. I was using `docker build - < docker/gpu.Dockerfile` but now I tried `docker build -f docker/gpu.Dockerfile .` and it works perfectly – John Henckel Aug 10 '21 at 21:12
  • Another big thanks here! I had my `Dockerfile` in a folder named `docker` and all files to be copied outside it in the root folder. I then run `docker build -t my-image .` The `dot` was there due to the `Dockerfile` being in this docker directory, but it couldn't see all the other files in the root folder because the context was set as the `docker` folder. This solved it: `docker build -t my-image -f ./Dockerfile ..` (executed within the `docker` folder). – Elias Kouskoumvekakis Jul 19 '23 at 17:00
6

I was also facing the same, I moved my docker file to root of the project. then it worked

Kumar Abhishek
  • 3,004
  • 33
  • 29
4

In your case removing ./ should solve the issue. I had another case wherein I was using a directory from the parent directory and docker can only access files present below the directory where Dockerfile is present so if I have a directory structure /root/dir and Dockerfile /root/dir/Dockerfile

I cannot copy do the following

COPY root/src /opt/src
Ankit Marothi
  • 955
  • 10
  • 14
  • 1
    My own problem was a little different, but Docker's "issue" with odd paths when referencing a file above the folder containing Dockerfile was the issue **THANKS**! [Windows 10 + Powershell]. – RichW Jul 24 '20 at 14:35
  • do you have a reference for this "docker can only access files present below the directory where Dockerfile is present" ? – user1689987 Jul 26 '22 at 03:10
3

In my case, it was the comment line that was messing up the COPY command

I removed the comment after the COPY command and placed it to a dedicated line above the command. Surprisingly it resolved the issue.

Faulty Dockerfile command

COPY qt-downloader .     # https://github.com/engnr/qt-downloader -> contains the script to auto download qt for different architectures and versions

Working Dockerfile command

# https://github.com/engnr/qt-downloader -> contains the script to auto download qt for different architectures and versions
COPY qt-downloader .     

Hope it helps someone.

1

This may help someone else facing similar issue.

Instead of putting the file floating in the same directory as the Dockerfile, create a dir and place the file to copy and then try.

COPY mydir/test.json /home/test.json
COPY mydir/test.json /home/test.json
Keyur Vyas
  • 191
  • 1
  • 6
1

Another potential cause is that docker will not follow symbolic links by default (i.e don't use ln -s).

Alex
  • 1,388
  • 1
  • 10
  • 19
1

The following structure in docker-compose.yaml will allow you to have the Dockerfile in a subfolder from the root:

version: '3'
services:
  db:
    image: postgres:11
    environment:
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - 127.0.0.1:5432:5432
  **web:
    build: 
      context: ".."
      dockerfile: dockerfiles/Dockerfile**
    command: ...
...

Then, in your Dockerfile, which is in the same directory as docker-compose.yaml, you can do the following:

ENV APP_HOME /home

RUN mkdir -p ${APP_HOME}

# Copy the file to the directory in the container
COPY test.json ${APP_HOME}/test.json
COPY test.py ${APP_HOME}/test.py

# Browse to that directory created above
WORKDIR ${APP_HOME}

You can then run docker-compose from the parent directory like:

docker-compose -f .\dockerfiles\docker-compose.yaml build --no-cache
gagneet
  • 35,729
  • 29
  • 78
  • 113
1

In my case, I had to put all my project files into a subdirectory

app -|inside app directory we have the following
     | package.js
     | src 
     | assets
Dockerfile

Then I copied files in his way

COPY app ./
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ntiyiso Rikhotso
  • 644
  • 6
  • 15
  • I had the same problem and this fixed it, `docker-compose version 1.24.0, build 0aa59064`, `Docker version 18.09.6, build 481bc77` – Tiana987642 Jul 26 '22 at 09:12
1

I had such error while trying to build a docker image and push to the container registry. Inside my docker file I tried to copy a jar file from target folder and try to execute it with java -jar command.

I was solving the issue by removing .jar file and target folder from .gitignore file.

Kaleab
  • 241
  • 2
  • 4
0

When using the Docker compose files, publish, publishes to obj/Docker/Publish. When I copied my files there and pointed my Dockerfile to this directory (as generated), it works…

Rıfat Erdem Sahin
  • 1,738
  • 4
  • 29
  • 47
0

The way docker look for file is from the current directory i.e. if your command is

COPY target/xyz.jar app.jar

ADD target/xyz.jar app.jar

The xyz jar should be in the current/target directory - here current is the place where you have your docker file. So if you have docker in a different dir. its better bring to main project directory and have a straight path to the jar being added or copied to the image.

Community
  • 1
  • 1
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19
0

I had the same issue with a .tgz file .

It was just about the location of the file. Ensure the file is in the same directory of the Dockerfile.

Also ensure the .dockerignore file directory doesn't exclude the file regex pattern.

0

In my case the solution was to place file in a directory and copy whole directory content with one command, instead of copying a single file:

COPY --chown=1016:1016 myfiles /home/myapp/myfiles
westman379
  • 493
  • 1
  • 8
  • 25
0

Make sure your path names are the same (case sensitive), folder name /dist/inventory

COPY /Dist/Inventory ... -- was throwing the error
COPY /dist/inventory ... -- working smoothly
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
0

Using nodejs/express/javascript!

In my case I had multiple CMD ["npm" "run"...] on the same Dockerfile, where you can only have 1. Hence, the first CMD ["npm" "run" "build"] was not being run while the /build folder was not created. Therefore the cmd to copy the build folder COPY --from=build /usr/src/app/build ./build failed! Change from a CMD to a RUN npm run build to fix the issue.

My Dockerfile:

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
# copy everything except content from .dockerignore
COPY . ./
#CMD ["npm", "run", "build"]
RUN npm run build
RUN ls -la | grep build

FROM node:lts-alpine3.17 as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
RUN pwd
COPY package*.json ./
RUN npm ci --only=production
COPY --from=build /usr/src/app/build ./build
CMD ["node", "build/index.js"]```
helvete
  • 2,455
  • 13
  • 33
  • 37
geuxor
  • 21
  • 7
-1

Here is the reason why it happens, i.e. your local directory in the host OS where you are running the docker should have the file, otherwise you get this error

One solution is to : use RUN cp <src> <dst> instead of COPY <src> <dst>

then run the command it works!

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • 1
    Hi, Nishant! It would be worthy to express your solution with more details. Copy files from host to container, is still something tricky on Docker. Please, give examples in order to clarify your answer. Regards :) – ivanleoncz Apr 28 '19 at 19:29
-8
 <plugin>
    <groupId>io.fabric8</groupId>

    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
      <images>
        <image>
          <name>imagenam</name>
          <alias>dockerfile</alias>
          <build>
            <!-- filter>@</filter-->
            <dockerFileDir>dockerfile loaction</dockerFileDir>
            <tags>
            <tag>latest</tag>
            <tag>0.0.1</tag>
            </tags>
          </build>
          <run>
            <ports>
              <port>8080:8080</port>
            </ports>
          </run>
        </image>
      </images>
    </configuration>
  </plugin>