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"]
Related
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 ..
I am trying to build an image by simply copying requirement.txt into it. My directory structure is as below:
- app
- Dockerfile
- requirement.txt
And my dockerfile looks like this :
FROM python:3.7
WORKDIR /home/app
COPY requirements.txt /home/app/
RUN pip install --no-cache-dir -r requirements.txt
I am ensuring that the requirement.txt file is inside docker build context and there is no .dockerignore file which could be possibly ignoring this file. But yet I am getting this below error.
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM python:3.7
---> 11c6e5fd966a
Step 2/4 : WORKDIR /home/app
---> Running in 5ea181f45c01
Removing intermediate container 5ea181f45c01
---> c150cd1dea37
Step 3/4 : COPY requirements.txt /home/app/
COPY failed: stat /var/lib/docker/tmp/docker-builder580673490/requirements.txt: no such file or directory
Command used for building the image : docker build -t test_app:0.1 .
Am I missing anything here?
WORKDIR switches working directory in docker image, but not in host.
COPY app/requirements.txt /home/app/
It works correctly.
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.
I am trying to package gotty into a Docker container but found a weird behavior.
$ tree
.
├── Dockerfile
├── gotty
└── gotty_linux_amd64.tar.gz
Dockerfile:
FROM alpine:3.11.3
RUN mkdir -p /home/gotty
WORKDIR /home/gotty
COPY gotty /home/gotty
RUN chmod +x /home/gotty/gotty
CMD ["/bin/sh"]
The image was built without issue:
[strip...]
Removing intermediate container 0dee1ab645e0
---> b5c6957d36e1
Step 7/9 : COPY gotty /home/gotty
---> fb1a1adec04a
Step 8/9 : RUN chmod +x /home/gotty/gotty
---> Running in 90031140da40
Removing intermediate container 90031140da40
---> 609e1a5453f7
Step 9/9 : CMD ["/bin/sh"]
---> Running in 30ce65cd4339
Removing intermediate container 30ce65cd4339
---> 099bc22ee6c0
Successfully built 099bc22ee6c0
The chmod changed the file mode successfully. So /home/gotty/gotty is present.
$ docker run -itd 099bc22ee6c0
9b219a6ef670b9576274a7b82a1b2cd813303c6ea5280e17a23a917ce809c5fa
$ docker exec -it 9b219a6ef670 /bin/sh
/home/gotty # ls
gotty
/home/gotty # ./gotty
/bin/sh: ./gotty: not found
Go into the container, the gotty command is there. I ran it with relative path. Why the not found?
You are running into one of the more notorious problems with Alpine: Musl, instead of glibc. Check out the output of ldd gotty. Try adding libc6-compat:
apk add libc6-compat
and see if that fixes it.
Been stuck on this for the last 3 days. I'm building an image in a docker and
copy command fails due to not finding the right directory.
FROM python:3.6.7-alpine
WORKDIR /usr/src/app
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip3 install -r requirements.txt
COPY . /usr/src/app
CMD python3 manage.py run -h 0.0.0.0
which is run by this docker-dev file:
version: '3.7'
services:
users:
build:
context: ./services/users
dockerfile: Dockerfile-dev
volumes:
- './services/users:/usr/src/app'
ports:
- 5001:5000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
and getting this error:
Building users
Step 1/6 : FROM python:3.6.7-alpine
---> cb04a359db13
Step 2/6 : WORKDIR /usr/src/app
---> Using cache
---> 06bb39a49444
Step 3/6 : COPY ./requirements.txt /usr/src/app/requirements.txt
ERROR: Service 'users' failed to build: COPY failed: stat /var/snap/docker/common/var-lib-docker/tmp/docker-builder353668631/requirements.txt: no such file or directory
I don't even know where to start with debugging this. When I tried to access the directory it gave me permission error. So I tried to run the command with sudo which didn't help. Any thoughts ?
Little late to reply, but second COPY command COPY . /usr/src/app replaces the /usr/src/app content generated by RUN pip3 install -r requirements.txt.
Try
FROM python:3.6.7-alpine
WORKDIR /usr/src/app
# install in temp directory
RUN mkdir /dependencies
COPY ./requirements.txt /dependencies/requirements.txt
RUN cd /dependencies && pip3 install -r requirements.txt
COPY . /usr/src/app
# copy generated dependencies
RUN cp -r /dependencies/* /usr/src/app/
CMD python3 manage.py run -h 0.0.0.0
As larsks suggests in his comment, you need the file in the services/users directory. To understand why, an understanding of the "context" is useful.
Docker does not build on the client, it does not see your current directory, or other files on your filesystem. Instead, the last argument to the build command is passed as the build context. With docker-compose, this context defaults to the current directory, which you will often see as . in a docker build command, but you can override that as you've done here with ./services/users as your context. When you run a build, the very first step is to send that build context from the docker client to the server. Even when the client and server are on the same host (a common default, especially for desktop environments), this same process happens. Files listed in .dockerignore, and files in parent directories to the build context are not sent to the docker server.
When you run a COPY or ADD command, the first argument (or all but the last argument when you have multiple) refer to files from the build context, and the last argument is the destination file or directory inside the image.
Therefore, when you put together this compose file entry:
build:
context: ./services/users
dockerfile: Dockerfile-dev
with this COPY command:
COPY ./requirements.txt /usr/src/app/requirements.txt
the COPY will try to copy the requirements.txt file from the build context generated from ./services/users, meaning ./services/users/requirements.txt needs to exist, and not be excluded by a .dockerignore file in ./services/users.
I had a similar problem building an image with beryllium, and I solved this deleting it into the .dockerignore
$ sudo docker build -t apache .
Sending build context to Docker daemon
10.55MB Step 1/4 : FROM centos ---> 9f38484d220f Step 2/4 :
RUN yum install httpd -y
---> Using cache ---> ccdafc4ae476 Step 3/4 :
**COPY ./**beryllium** /var/www/html COPY failed: stat /var/snap/docker/common/var-lib-docker/tmp/docker-builder04301**
$nano .dockerignore
startbootstrap-freelancer-master
run.sh
pro
fruit
beryllium
Bell.zip
remove beryllium from that file
$ sudo docker build -t apache .
Sending build context to Docker daemon 12.92MB
Step 1/4 : FROM centos
---> 9f38484d220f
Step 2/4 : RUN yum install httpd -y
---> Using cache
---> ccdafc4ae476
Step 3/4 : COPY ./beryllium /var/www/HTML
---> 40ebc02992a9
Step 4/4 : CMD apachectl -DFOREGROUND
---> Running in dab0a406c89e
Removing intermediate container dab0a406c89e
---> 1bea741cfb65
Successfully built 1bea741cfb65
Successfully tagged apache:latest