Can't create a docker image for COPY failed: stat /var/lib/docker/tmp/docker-builder error

PythonDockerDockerfileDocker Image

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

!mydir/test.json
!mydir/test.py

Solution 3 - Python

  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

Solution 4 - Python

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

docker build .

Solution 5 - Python

Removing ./ from source path should resolve your issue:

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

Solution 6 - Python

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

Solution 7 - Python

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.

Solution 8 - Python

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.

Solution 9 - Python

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

Solution 10 - Python

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

Solution 11 - Python

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

Solution 12 - Python

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.

Solution 13 - Python

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…

Solution 14 - Python

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

Solution 15 - Python

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.

Solution 16 - Python

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.

Solution 17 - Python

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 ./

Solution 18 - Python

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

Solution 19 - Python

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!

Solution 20 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdoBenView Question on Stackoverflow
Solution 1 - PythonIvan LeeView Answer on Stackoverflow
Solution 2 - PythonBrahimi boubakeurView Answer on Stackoverflow
Solution 3 - PythonlupguoView Answer on Stackoverflow
Solution 4 - PythonTadejView Answer on Stackoverflow
Solution 5 - PythonDaniel RogatchevskyView Answer on Stackoverflow
Solution 6 - PythonKumar AbhishekView Answer on Stackoverflow
Solution 7 - PythonKevinView Answer on Stackoverflow
Solution 8 - PythonMohammed MuneerView Answer on Stackoverflow
Solution 9 - PythonAnkit MarothiView Answer on Stackoverflow
Solution 10 - PythonKeyur VyasView Answer on Stackoverflow
Solution 11 - PythonAlexView Answer on Stackoverflow
Solution 12 - PythonKaleabView Answer on Stackoverflow
Solution 13 - PythonRıfat Erdem SahinView Answer on Stackoverflow
Solution 14 - PythongagneetView Answer on Stackoverflow
Solution 15 - PythonPravin BansalView Answer on Stackoverflow
Solution 16 - PythonAbdurrahman AdebiyiView Answer on Stackoverflow
Solution 17 - PythonNtiyiso RikhotsoView Answer on Stackoverflow
Solution 18 - Pythonwestman379View Answer on Stackoverflow
Solution 19 - PythonNishant MishraView Answer on Stackoverflow
Solution 20 - PythonSatish KoppisettyView Answer on Stackoverflow