COPY statement does not actually copy my file

Hello all,

I have packaged a MATLAB Standalone application into a Docker image via the following function on a Linux VM running on a Windows host:

Now, I want to add some files to this before i run my Docker image as a container. To do this I have written the following Dockerfile:

#dockerfile_for_adding_files
#---------------------------------------------------------------------------------------------------
FROM matlab-server:latest

#Add new files to the docker image
COPY test.m /root/server_folder/matlab_server_folder/test.m
#---------------------------------------------------------------------------------------------------

To run this Dockerfile, I use the following command:

docker build -t matlab-server:new dockerfile-for-adding-files

However, when I run my container using the following line, I cannot find the files in the container filesystem via the terminal:

docker run --rm -p 9002:9002 matlab-server:new

What am I doing wrong? How can I get the new files to end up in my container?

Best

It looks like your Dockerfile is correct, so it’s possible that the issue is with the location of the files within the container. You are copying the files to the /root/server_folder/matlab_server_folder/ directory in the container, so you need to make sure that the MATLAB application is looking for the files in that directory as well.

If the application is looking for the files in a different directory, you may need to modify your Dockerfile to copy the files to the appropriate directory. Additionally, you can try running the container with the -it flag to open an interactive terminal session, which will allow you to navigate the container filesystem and verify that the files are in the correct location.

Here is an example command to run the container with the -it flag:

docker run --rm -p 9002:9002 -it matlab-server:new /bin/bash

Once inside the container, you can navigate to the /root/server_folder/matlab_server_folder/ directory and verify that the files are present using the ls command. If the files are not present, you may need to modify your Dockerfile or double-check the file paths.

Hello Ajeet!

Many thanks for your reply.

Unfortunately I have already tried to ls within the container filesystem in order to check if the files are there, and I cannot see them. I have also tried using several different folders. Could there be some issue with a .dockerignore file or some permission issue when trying to write the files in the container filesystem? How would you suggest I try modifying the Dockerfile?

Best
Julius

If you’re using a .dockerignore file, make sure that it’s not excluding the files you’re trying to copy to the container. You can try temporarily removing the .dockerignore file to see if that resolves the issue.

Also, can you try the below Dockerfile for debugging purpose.


FROM matlab-server:latest

#Add new files to the docker image
COPY test.m /root/server_folder/matlab_server_folder/test.m

# Debugging commands:
RUN ls /root/server_folder/matlab_server_folder/ # Verify that the file is in the target directory
RUN echo "Hello, World!" > /root/server_folder/matlab_server_folder/test.txt # Write a test file to the target directory to check for permission issues
RUN cat /root/server_folder/matlab_server_folder/test.txt # Verify that the test file was written successfully

Dear Ajeet,

I have not explicitly defined a .dockerignore-file, so I assume that I am not using one.

I ran the suggested Dockerfile, and it runs without failure. One peculiar thing that I noticed was however that when the ls-command is run, the only files that exist in this folder are the files that i added in the Dockerfile build. None of the files that are there when I Is to this directory when running the container were listed. Again, when i run the container, the files are not there. Any idea about what could be going wrong when actually running the container?

Best

This name is not mentioned in the documentation you shared. Please, share the Dockerfile that you used to build the base image unless it contains something that you can’t share. Sharing only a documentation would require us to check it, understand it and figure out what you did differently when we might not even know the tool that you are running in the container.

Also to avoid misunderstanding, please, format your codes as described in the bellow post:

How do you know that the file is not there? If you can share commands that actually shows what command indicated the file was missing, that can help us a lot to help you faster and ask better questions. Later you mentioned the “ls” command, but we all make mistakes, so the fact that you used “ls” doesn’t guarantee you used it correctly.

Can I ask why you suspected the reason could have been a dockerignore file when you don’t have one? Only to make sure we understand eachother correctly. Note that the filename would be .dockerignore and not .dockerignore-file or anything like that. If something created a .dockerignore file it is possible that you don’t see it because those files beginning with a dot are hidden by default.

However you also mentioned that

which indicates that indeed you don’t have a .dockerignore file or doesn’t match the files that you want to copy.

I also need to ask you to share the output of the following commands as it is very important to know what kind of Docker installation you are using.

docker info
docker version

You could also check if you have any ONBUILD instruction in the Dockerfile of the base image so it runs after your instructions and can remove the copied files.

Was this the exact same command that you run? Didn’t you use any mounted folder?

Another reason of missing files could be that a command runs in your entrypoint removing the copied files. To help you with that, again, we need more information how you have built the base image exactly.

By the way, you chose the “Docker Hub” category and the question has nothing to do with Docker Hub, so based on the output of docker info we can move the topic to the correct category.