dockerfile COPY does not copy correctly - docker

I have a directory:
and a Docerfile:
from rocker/rstudio
COPY . .
when I look in the root directory, I cannot find any copied files in the root, nor in the rstudio directory
what is going on?
I looked at other links Docker copy command does not copy file, Dockerfile COPY instruction failing?, dockerfile COPY not copying file, but cannot find an answer.
Have I missed something obvious?

Related

Clarify how docker command COPY behaves relative to docker build context

I want to copy the contents of a parent directory (relative to the position of the Dockerfile) into my image.
This is the folder structure:
app-root/
docker/
php81aws/
some-folder
Dockerfile
start-container
supervisord.conf
app_folders
app_files
I'm calling docker build as follows:
app-root#> docker build -t laravel -f docker/php81aws/Dockerfile .
Or from docker compose with:
services:
laravel:
build:
context: ./
dockerfile: docker/php81aws/Dockerfile
Therefore, the context should be in the app-root directory.
In the dockerfile, I'm using COPY like so:
COPY docker/php81aws/start-container /usr/local/bin/start-container
COPY docker/php81aws/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod +x /usr/local/bin/start-container
COPY --chown=www:www . /var/www/
It always gives me this error:
failed to compute cache key: "/start-container" not found: not found
I tried to use COPY ./docker/php81aws/start-container and COPY start-container but the error is always the same. Of course copying the parent directory also fails.
You mention in a comment that your top-level app-root directory has a .dockerignore file that excludes the entire docker directory. While the Dockerfile will still be available, nothing else in that tree can be COPYed into the image, and if you COPY ./ ./ to copy the entire build context into the image, that directory won't be present.
Deleting this line from the .dockerignore file should fix your issue.
In general you want the .dockerignore file to include anything that's part of your host build-and-test environment, but should not by default be included in an image, possibly because the Dockerfile is going to rebuild it. In a Node context, for example, you almost always want to exclude the host's node_modules directory, or in a Java context often the Gradle build or Maven target directories. Anything you do want to include in the image needs to not be listed in .dockerignore.

Docker: Ignore some files which are needed in build context

I have a folder : /docker on my project root and it contains certain files like Apache configs which I need to copy to the correct folders during teh build.
Project-root
------docker folder
-----------apache.conf
Now
DOCKERFILE
In my dockerfile i have a copy command which copies the whole of root to the container.
`Issue`
I don't want the docker folder to be copied to the built image.
I added docker to the .dockerignore. But this leads to the problem that these files are not sent to the build context and then the build command fails.
WORKAROUND
Use the RUN command in the dockerfile to remove the docker folder instead of using .dockerignore
QUESTION
Is there any better solution?
DOCKER FILe:
FROM <baseimage>
WORKDIR /var/www/html/
COPY . /var/www/html/MYFOLDER
COPY ./docker/apache.conf /etc/httpd/conf/apache.conf
COPY ./docker/sites-enabled /etc/httpd/sites-enabled

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.

What is the easiest method to copy files and directories in dockerfile

I tried to copy some files from source to destination (flask app) in a dockerfile but it seems things are not working as expected when building the image. With last 2 line showing:
Step 3 : COPY pkl_objects/* /home/jovyan/work/movieclassifier/pkl_objects/
No source files were specified
This is the docker file.
FROM jupyter/datascience-notebook
RUN pip install flask flask-wtf
COPY pkl_objects/* /home/jovyan/work/movieclassifier/pkl_objects/
COPY static/* /home/jovyan/work/movieclassifier/static/
COPY templates/* /home/jovyan/work/movieclassifier/templates/
COPY app.py /home/jovyan/work/movieclassifier
COPY reviews.sqlite /home/jovyan/work/movieclassifier
COPY vectorizer.py /home/jovyan/work/movieclassifier
WORKDIR /home/jovyan/work/movieclassifier
ENV FLASK_APP=app.py
# ENV FLASK_DEBUG=0
CMD ["flask", "run", "--host=0.0.0.0"]
Looks like there are no files in the pkl_objects folder, and when the wildcard (*) is expanded, it results in no source files being specified.
Maybe you could add an empty file in there, so that when the wildcard picks up files, you at least get one source file.
Example file could be: .nonempty or something like that.

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