Docker cannot find file or directory with copy command - docker

So I am pretty new to docker and it's setup but I have been messing around with this dockerfile for about half an hour trying to get a working image together for an angular 6 application that I have.
Aside from the messy dockerfile the last bit doesn't seem to be working.
So I am working inside of the /workdir directory for all this stuff, building my angular app, etc. Afterwards I move out to the root directory and then want to copy the /workdir/dist directory to the root /dist folder and then delete the working directory. I get a file or directory not found at the copy line. However if I run ls I see a workdir exists and if I run ls ./workdir/dist I see that there are files in it.
My linux scripting and docker understanding is very limited but I cannot see why this seems to fail.
FROM node:8
ENV ExposedPort 80
WORKDIR /workdir
COPY . /workdir
RUN npm install
RUN npm run production-angular
COPY package.json dist
COPY index.js dist
WORKDIR /
RUN ls
RUN ls workdir
RUN ls workdir/dist
COPY workdir/dist dist
CMD node index.js
EXPOSE ${ExposedPort}
My last few lines from the docker build command:
Step 9/15 : WORKDIR /
Removing intermediate container 10f69d248a51
---> d6184c6f0ceb
Step 10/15 : RUN ls
---> Running in c30b23783655
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
workdir
Removing intermediate container c30b23783655
---> fb74727468f6
Step 11/15 : RUN ls workdir
---> Running in e6bae18e3560
README.md
angular.json
dist
dockerfile
e2e
index.js
node_modules
package-lock.json
package.json
src
tsconfig.json
tslint.json
Removing intermediate container e6bae18e3560
---> c3e1f9f77d00
Step 12/15 : RUN ls workdir/dist
---> Running in 07923b11226e
3rdpartylicenses.txt
favicon.ico
index.html
index.js
main.1c74cb5a2b3da54e1148.js
package.json
polyfills.479875dfe4ba221bc297.js
runtime.a66f828dca56eeb90e02.js
styles.34c57ab7888ec1573f9c.css
Removing intermediate container 07923b11226e
---> 66ef563ba292
Step 13/15 : COPY workdir/dist dist
COPY failed: stat /var/lib/docker/tmp/docker-builder326636018/workdir/dist: no such file or directory

Change COPY ./workdir to COPY workdir
https://github.com/docker/for-linux/issues/90#issuecomment-326045261
Paths in a Dockerfile are always relative to the the context directory. The context directory is the positional argument passed to docker build (often .).
If there is no ./tmp in the context directory then this is the expected behaviour.

Related

Error copying folder using COPY --from from another layer

The Dockerfile uses the COPY --from command from the other build Node layer, but the generated directory is not found.
Note 1: This Dockerfile works locally on my machine doing builds normally.
Note 2: In the execution log it mentions the removal of an intermediate container, is that it? Would it be possible to preserve this container so that the copy works?
FROM node:16.16 as build
# USER node
WORKDIR /app
COPY package.json /app
RUN npm install --location=global npm#latest && npm install --silent
COPY . .
ARG SCRIPT
ENV SCRIPT=$SCRIPT
ARG API_URL
ENV API_URL=$API_URL
ARG API_SERVER
ENV API_SERVER=$API_SERVER
CMD ["/bin/sh", "-c", "envsubst < src/proxy.conf.template.js > src/proxy.conf.js"]
RUN npm run ${SCRIPT}
FROM nginx:1.23
VOLUME /var/cache/nginx
EXPOSE 80
COPY --from=build /app/dist/siai-spa /usr/share/nginx/html
COPY ./config/nginx-template.conf /etc/nginx/nginx-template.conf
b9ed43dcc388: Pull complete
Digest: sha256:db345982a2f2a4257c6f699a499feb1d79451a1305e8022f16456ddc3ad6b94c
Status: Downloaded newer image for nginx:1.23
---> 41b0e86104ba
Step 15/24 : VOLUME /var/cache/nginx
---> Running in dc0e24ae6e51
Removing intermediate container dc0e24ae6e51
---> 3b2799dad197
Step 16/24 : EXPOSE 80
---> Running in f30edd617285
Removing intermediate container f30edd617285
---> 21985745ce49
Step 17/24 : COPY --from=build /app/dist/siai-spa /usr/share/nginx/html
COPY failed: stat app/dist/siai-spa: file does not exist
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1
I guess, you should use CMD instead of RUN while npm run ${SCRIPT} as this needs to be executed during container running time rather than image build time.
Solved problem!
The difference was that locally I used docker-compose which captures the build arguments from the .env file. The npm run command did not override the ${SCRIPT} variable as the docker command does not use the env file, required to pass through the --build-arg parameters.

is it possible to allow failure in COPY at a Dockerfile?

I have a multistage Docker file like this:
From A as a
WORKDIR /app
COPY . .
From B as b
COPY --from=a /app/path/to/a/file /destination/file/path
Sometimes the source-file of last COPY does not exist and this causes a failure in docker build. Is it possbile to do the magic w/o worrying about the existence of /app/path/to/a/file?
I don't suppose there is a way to allow for a failure in a COPY instruction, but what you could do is copy not a specific file, but the content of the folder that does exist (even if it's empty).
For example, imagine you have a folder structure like this
.
├── Dockerfile
└── test_folder
└── test_file
1 directory, 2 files
and you build from this Dockerfile:
FROM alpine as alp1
WORKDIR /app
COPY . .
FROM alpine
RUN mkdir /dest_folder
COPY --from=alp1 /app/test_folder/test_file /dest_folder
if works because the file does exist and if we were to delete the file from that folder, the COPY would fail.
So instead of copying /app/test_folder/test_file we could copy /app/test_folder/, which will copy EVERYTHING from inside the test_folder to /dest_folder in the second container, EVEN if there is nothing:
file removed:
.
├── Dockerfile
└── test_folder
1 directory, 1 file
building from:
FROM alpine as alp1
WORKDIR /app
COPY . .
FROM alpine
RUN mkdir /dest_folder
COPY --from=alp1 /app/test_folder/ /dest_folder
> docker build -t test .
Sending build context to Docker daemon 2.56kB
Step 1/6 : FROM alpine as alp1
---> 0a97eee8041e
Step 2/6 : WORKDIR /app
---> Using cache
---> 0a6fc3a90e15
Step 3/6 : COPY . .
---> Using cache
---> 076efaa3a8b9
Step 4/6 : FROM alpine
---> 0a97eee8041e
Step 5/6 : RUN mkdir /dest_folder
---> Using cache
---> 8d647b9a1573
Step 6/6 : COPY --from=alp1 /app/test_folder/ /dest_folder
---> Using cache
---> 361b0919c585
Successfully built 361b0919c585
Successfully tagged test:latest
dest_folder exists:
docker run -it --rm test ls
bin etc media proc sbin tmp
dest_folder home mnt root srv usr
dev lib opt run sys var
but nothing is inside
docker run -it --rm test ls -lah /dest_folder
total 8K
drwxr-xr-x 1 root root 4.0K Nov 24 14:06 .
drwxr-xr-x 1 root root 4.0K Nov 24 14:13 ..

docker chmod no such file or directory

hi i need to chmod this file /root/Desktop/folderdocker/index.php
using chmod 774 command
here my dockerfile:
FROM php:7.4-cli
copy . index.php
RUN chmod -R 774 /Dekstop/folderdocker/index.php
RUN chown -R root /var/www
output:
Sending build context to Docker daemon 342.9MB
Step 1/4 : FROM php:7.4-cli
---> f4f453029716
Step 2/4 : copy . index.php
---> Using cache
---> bc9a68fff22f
Step 3/4 : RUN chmod -R 774 /Dekstop/folderdocker/index.php
---> Running in 4ddc85713576
chmod: cannot access '/Dekstop/folderdocker/index.php': No such file or directory```
You are copying the directory of the Dockerfile into a dir called index.php in the root of your image. Then you are referencing a file in a path that does not exist.
Make sure index.php is next to your Dockerfile, for the next command to work, or you would need to modify the source path.
You should COPY index.php /some/path/or/just/root/index.php make sure you RUN mkdir if the path does not exist. And then you can chown the file.
In order to be able to see the image you are inheriting from you can run:
docker run -it php:7.4-cli sh to get a shell in it and see the available dirs.

Docker copies the file but doesnt exist while running

When i try to build the Dockerfile, it copies the sample.pdf from documents folder. But the pdf file doesnt exist in container when i run it.
Step 6/9 : COPY . .
---> b0c137c4b5bb
Step 7/9 : COPY documents/ /usr/src/app/documents/
---> 77ac91c3ebb9
Step 8/9 : RUN ls -la /usr/src/app/documents/*
---> Running in 03c9f14669c3
-rw-rw-rw- 1 root root 2830 May 3 14:30 /usr/src/app/documents/sample.pdf
Removing intermediate container 03c9f14669c3
After running the docker-compose image.
The image doesnt exist in container.
sudo docker exec -it test_consumer_1 ls /usr/src/app/documents
//[None] - it should show sample.pdf
Dockerfile:
FROM python:3.6-alpine
COPY requirements.txt /usr/src/app/requirements.txt
WORKDIR /usr/src/app
RUN pip install -r requirements.txt
# Without this setting, Python never prints anything out.
ENV PYTHONUNBUFFERED=1
COPY . .
COPY documents/ /usr/src/app/documents/
RUN ls -la /usr/src/app/documents/*
CMD ["python", "receive.py"]

no such file or directory in COPY dockerfile

I have made the following dockerfile to contain my node js application, the problem is that an error appears when building the dockerfile:
Sending build context to Docker daemon 2.048kB
Step 1/7 : FROM node:10
---> 0d5ae56139bd
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> 5bfc0405d8fa
Step 3/7 : COPY package.json ./
COPY failed: stat /var/lib/docker/tmp/docker-
builder803334317/package.json: no such file or directory
this is my dockerfile:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
i executed the command:
sudo docker build - < Dockerfile
into my root project folder.
My project folder is simple, like this:
-Project
-app.js
-Dockerfile
-package.json
-package-lock.json
-README.md
I am doing something wrong?
When you use the Dockerfile-on-stdin syntax
sudo docker build - < Dockerfile
the build sequence runs in a context that only has the Dockerfile, and no other files on disk.
The directory layout you show is pretty typical, and pointing docker build at that directory should work better
sudo docker build .
(This is the same rule as the "Dockerfiles can't access files in parent directories" rule, but instead of giving the current directory as the base directory to Docker, you're giving no directory at all, so it can't even access files in the current directory.)

Resources