0

Hi I have a docker file which is failing on the COPY command. It was running fine initially but then it suddenly crashed during the build process. The Docker file basically sets up the dev environment and authenticate with GCP.

FROM ubuntu:16.04

## ENV Variables
ENV PYTHON_VERSION="3.6.5"
ENV BUCKET_NAME='detection-sandbox'
ENV DIRECTORY='/usr/local/gcloud'

# Update and Install packages
RUN apt-get update -y \
 && apt-get install -y \
 curl \
 wget \
 tar \
 xz-utils \
 bc \
 build-essential \
 cmake \
 curl \
 zlib1g-dev \
 libssl-dev \
 libsqlite3-dev \
 python3-pip \
 python3-setuptools \
 unzip \
 g++ \
 git \
 python-tk

 # Install Python 3.6.5
RUN wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz \
    && tar -xvf Python-${PYTHON_VERSION}.tar.xz \
    && rm -rf Python-${PYTHON_VERSION}.tar.xz \
    && cd Python-${PYTHON_VERSION} \
    && ./configure \
    && make install \
    && cd / \
    && rm -rf Python-${PYTHON_VERSION}

# Install pip
RUN curl -O https://bootstrap.pypa.io/get-pip.py \
    && python3 get-pip.py \
    && rm get-pip.py

# Add SNI support to Python
RUN pip --no-cache-dir install \
        pyopenssl \
        ndg-httpsclient \
        pyasn1

## Download and Install Google Cloud SDK
RUN mkdir -p /usr/local/gcloud \
    && curl https://sdk.cloud.google.com > install.sh \
    && bash install.sh --disable-prompts --install-dir=${DIRECTORY}

# Adding the package path to directory
ENV PATH $PATH:${DIRECTORY}/google-cloud-sdk/bin

# working directory
WORKDIR /usr/src/app

COPY requirements.txt ./ \
     testproject-264512-9de8b1b35153.json ./

It fails at this step :

Step 13/21 : COPY requirements.txt ./      testproject-264512-9de8b1b35153.json ./
COPY failed: stat /var/lib/docker/tmp/docker-builder942576416/testproject-264512-9de8b1b35153.json: no such file or directory

Any leads in this would be helpful.

CyberPunk
  • 1,297
  • 5
  • 18
  • 35
  • 1
    `COPY failed: stat /var/lib/docker/tmp/docker-builder942576416/testproject-264512-9de8b1b35153.json: no such file or directory` means that the file testproject-264512-9de8b1b35153.json was not found in the build context. If it was working before, you most probably deleted that file. – Zeitounator Jan 16 '20 at 14:23
  • 1
    You want to copy 2 local files to the image right? Not sure this works as you pasted in your example ... try out like explained here https://stackoverflow.com/a/30316600/65456 – Toni Van de Voorde Jan 16 '20 at 14:26
  • @Zeitounator I can see this file in my directory. I just moved the the Dockerfiles and this file to a new project directory. – CyberPunk Jan 16 '20 at 14:26
  • Then you need to find out why you think the file is there and docker thinks it is not. Hint: human is usually wrong, computer is almost always right. – Zeitounator Jan 16 '20 at 14:34
  • @Zeitounator yes that's why I am having a hard time diagnosing it since I can see it in the project directory but for some reason docker thinks its not in the context. – CyberPunk Jan 16 '20 at 14:37
  • ... and as pointed out by @ToniVandeVoorde, the correct syntax is `COPY src1 src2... dest` and not `srca desta srcb destb`. For the latest, you need two seperate copy commands. – Zeitounator Jan 16 '20 at 14:38
  • @Zeitounator I used the other syntax as well : COPY requirements.txt testproject-264512-9de8b1b35153.json ./ , no luck – CyberPunk Jan 16 '20 at 14:57

2 Answers2

3

How are you running docker build command?

In docker best practices I've read that docker fails if you try to build your image from stdin using -

Attempting to build a Dockerfile that uses COPY or ADD will fail if this syntax is used. The following example illustrates this:

# create a directory to work in
mkdir example
cd example

# create an example file
touch somefile.txt

docker build -t myimage:latest -<<EOF
FROM busybox
COPY somefile.txt .
RUN cat /somefile.txt
EOF

# observe that the build fails
...
Step 2/3 : COPY somefile.txt .
COPY failed: stat /var/lib/docker/tmp/docker-builder249218248/somefile.txt: no such file or directory

I've reproduced issue... Here is my Dockerfile:

FROM alpine:3.7

## ENV Variables
ENV PYTHON_VERSION="3.6.5"
ENV BUCKET_NAME='detection-sandbox'
ENV DIRECTORY='/usr/local/gcloud'

# working directory
WORKDIR /usr/src/app

COPY kk.txt ./ \
     kk.2.txt ./

If I create image by running docker build -t testImage:1 [DOCKERFILE_FOLDER], docker creates image and works correctly.

However if I try the same command from stdin as:

docker build -t test:2 - <<EOF
FROM alpine:3.7

ENV PYTHON_VERSION="3.6.5"
ENV BUCKET_NAME='detection-sandbox'
ENV DIRECTORY='/usr/local/gcloud'

WORKDIR /usr/src/app

COPY kk.txt ./      kk.2.txt ./
EOF

I get the following error:

Step 1/6 : FROM alpine:3.7
 ---> 6d1ef012b567
Step 2/6 : ENV PYTHON_VERSION="3.6.5"
 ---> Using cache
 ---> 734d2a106144
Step 3/6 : ENV BUCKET_NAME='detection-sandbox'
 ---> Using cache
 ---> 18fba29fffdc
Step 4/6 : ENV DIRECTORY='/usr/local/gcloud'
 ---> Using cache
 ---> d926a3b4bc85
Step 5/6 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 57a1868f5f27
Step 6/6 : COPY kk.txt ./      kk.2.txt ./
COPY failed: stat /var/lib/docker/tmp/docker-builder518467298/kk.txt: no such file or directory

It seems that docker build images from /var/lib/docker/tmp/ if you build image from stdin, thus ADD or COPY commands don't work.

Alvaro Niño
  • 547
  • 1
  • 4
  • 17
  • I am using the following command : docker build -t test-docker . – CyberPunk Jan 16 '20 at 14:47
  • what's your working directory, could you use complete path in command instead of `.`? e.g `docker build -t test:1 /home/anino/kk` – Alvaro Niño Jan 16 '20 at 14:49
  • I tried with the full path but still the same error :/ – CyberPunk Jan 16 '20 at 14:56
  • Could you try in another machine?, I've tested your Dockerfile as it is and it works (if files `testproject-264512-9de8b1b35153.json` and `requirements.txt` exists in the same `Dockerfile` path)... You should have some missconfiguration in your build machine and/or file `testproject-264512-9de8b1b35153.json` doesn't exists in the same Dockerfile path... – Alvaro Niño Jan 16 '20 at 15:10
  • I got it know, in my .dockerignore I had excluded .json files , now its resolved, thank you :) – CyberPunk Jan 17 '20 at 08:36
1

Incorrect path in source is a common error.

Use

COPY ./directory/testproject-264512-9de8b1b35153.json /dir/

instead of

COPY testproject-264512-9de8b1b35153.json /dir/
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31