Docker doesn't COPY files to created directories - docker

I'm kind of new to Docker, and I'm running into an issue I can't find a straightforward explanation for. I have a pretty simple Dockerfile that I'm building into an image, but when I deploy the image to Kubernetes as a pod, the results aren't what I expect.
FROM ubuntu:16.04
RUN mkdir workspace
WORKDIR /workspace
COPY . /workspace
CMD ["ls"]
When I check the logs for the deployment, there are no files listed in the /workspace folder, even though the folder itself exists. However, if I change my COPY's destination to a default linux folder like /usr, the files are there as I'd expect. My suspicion is that this has something to do with storage persistence, but since I'm copying the files into the folder when I build my image and the folder persists in the pod, I'm at a loss for why this happens. Any guidance would be greatly appreciated.

I would venture to guess that the ubuntu:... image doesn't have a WORKDIR set to /, and hence your copy command isn't working as expected.
Try changing the run command to be RUN mkdir /workspace and I think you'll see what you expected.
Hope this helps.

Related

Docker wrongfully uses a cached Dockerfile step?

I have a following Dockerfile
FROM solr:9
ARG configsets
ADD ${configsets} /opt/solr/server/solr/configsets
RUN mkdir -v -p /var/solr/data && chown solr:solr /var/solr/data
ENTRYPOINT ...
If I build this locally, it works fine. If I build this from ssh inside a CI server, I see that the built image doesn't have /var/solr/data folder created. This mkdir command wasn't present previously (I added it to control folder permissions when a volume is mounted on that path).
The only hypothesis I have, is that docker cached layers from old Dockerfile build, and for whatever reason it fails to understand that new Dockerfile differs from the old one at the point of RUN mkdir part. But why? How can I avoid this? I want to understand robust futureproof solution, and not just adhoc fixing this specific case by deleting cached layers. I don't want to disable caching altogether. I'm largely fine that the dependency image (ie., solr:9) could have a potentially stale cache, it's not an issue to me. Only stale commands in my own Dockerfile are fatally problematic.

Extending docker image

I want to extend the existing redis:6.0-alpine image from docker-hub and want to add my configuration file in the container.
For that I've created this dockerfile
FROM redis:6.0-alpine
WORKDIR .
COPY redis.master.conf ./config/redis.conf
but when building a container from this image, there is nothing copyed at the specified location.
Setup:
wsl2 (ubuntu 18.04 distro)
windows 10
docker-for-windows (v20.10.2)
Some help ?
Just tested myself, and it's copied without issues.
Where are you trying to look for the file? Notice the entry directory of this image is not /, it's /data, hence your file is on /data/etc/redis.conf
This is because WORKDIR is set to . in your dockerfile, which means current working directory. If you look at the official dockerfile of redis:6.0-alpine, the WORKDIR is set to /data.
Hence according to your dockerfile, you are copying the redis.master.conf file to ./ which means /data. Please check your file at /data/etc/redis.conf. I tried this at my end and can confirm this behavior.
If you want to copy it at /etc/redis.conf then remove ./ before /etc.

Docker starts looking for src folder in wrong directory

So, I am new to Docker but after trying a couple of times I can't get this to work.
Basically I have a simple script like this:
Dockerfile
from centos:7
.....
COPY /C:/Users/Kalin/Drive/web/sites/foo.com/ /var/www
EXPOSE 80
Whenever I run docker build everything works as planned except at the COPY part I get this error:
Step 10/11 : COPY /C:/Users/Kalin/Drive/web/sites/foo.com/ /var/www
COPY failed: stat /var/lib/docker/tmp/docker-builder646917435/C:/Users/Kalin/Drive/web/Kalin/foo.com: no such file or directory
I get the error is about not finding a directory, but instead of looking for it starting from C:/.. foldedr it is looking from /var/...
I don't know what mistake I am doing
I would strongly suggest to make relative path in COPY command so if you have your docker file in /C:/Users/Kalin and you are running docker build from that folder, just place Drive/web/sites/foo.com/ in COPY command

Dockerfile COPY from a Windows file system to a docker container

I have a simple Dockerfile:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
It is the last line that is giving me problems. I am copying from a Windows structure to a docker container (Linux I assume). When I build this image I get:
...
Step 3/3 : COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
COPY failed: stat /var/lib/docker/tmp/dockerbuilder720374851/C:/Users/rburton/code/MyAgsourceAPI: no such file or directory
First, something is preventing the recognition that this is an absolute path and naturally if the path is pre-pended with /var/lib/docker/tmp/dockerbuilder720374851 then the file will not be found. Second, I have tried / and \ but all with the same result. Also the drive letter I suspect is confusing to docker. So the question is how do I copy files and folders (along with the contents) from a Windows folder to a docker container?
First, change your Dockerfile to:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY MyAgsourceAPI /var/www
Then, to go your code directory: cd Users/rburton/code.
Within that directory, run:
docker build -t <image_name> .
Another tip that might be helpful, I've seen same issue while running build from correct context, and issue remained until I've used all small caps on src folder that I wanted to copy from. eg:
COPY MyAgsourceAPI /var/www -> COPY myagsourceapi /var/www
The root of the path is relative to the Dockerfile and not your Windows filesystem.
If for example your filesystem is layed out like this:
+-+-MyProject
|
+---Dockerfile
|
+-+-build
|
+---MyAgsourceAPI
You can use:
COPY /build/MyAgsourceAPI /var/www
Note that "MyProject" (or anything above it) is excluded from the path.

Wrong copy of folder in Dockerfile

I try to copy my build folder to /usr/share/nginx/html
So I want to have as result:
/usr/share/nginx/html/build/xxx
I perform this inside my dockerfile
cp -r build /usr/share/nginx/html/
But then than all the content of my build/ folder is copied inside /usr/share/nginx/html and not the folder itself
So like: /usr/share/nginx/html/xxx
When I perform the exactly same command inside my running container it happens in the right way!?
Than I got /usr/share/nginx/html/build/xxx
Can someone tell me what I'm doing wrong?
I don't know how exactly the Docker daemon works during image building but according to the documentation for COPY there is a clause that says;
COPY
If is a directory, the entire contents of the directory are
copied, including filesystem metadata. Note: The directory itself is
not copied, just its contents.
Obviously we are talking about the Linux cp command and not docker's COPY but it sounds like the rule somehow seems to have been applied to cp as well?
See:
https://docs.docker.com/engine/reference/builder/#/copy
For fixing your problem - probably just use the ADD command to copy your build directory?
Example: (Assuming your build directory is at the same level as Dockerfile;
ADD build /usr/share/nginx/html/
Let me know if this works for you.

Resources