COPY is not working in Docker

+1 vote

Below is my Dockerfile

#DockerfileCron    
FROM node:6

RUN mkdir /www
COPY . /www

WORKDIR /www

RUN apt-get update && apt-get install -y cron

CMD ["cron", "-f"]

When I build an image based on this Dockerfile, the command COPY just copy Dockerfile and ignore all other files in the folder.

# construção das imagens
docker build -t job/job_cronjob - < DockerfileCron

# executa o cron
docker run -d \
        --name job_cronjob job/job_cronjob

Can anyone help me with this?

Jun 22, 2018 in Docker by Damon Salvatore
• 5,510 points
1,798 views

Does it work if you copy the file using the cp command on the console?

Yes It works! Why does it not work when I do it using a Dockerfile?
Try using ADD instead of COPY.
That did not work either
@Anjana, that's a very wrong approach here. ADD has a very similar syntax and does something similar to COPY but in this case using ADD is not the correct thing to do.
When ADD and COPY have a similar syntax and does almost the same thing, How is it a wrong approach? Also, can you explain when exactly yo use COPY and when ADD?

Have a look at this thread and this blog which explains your query.

4 answers to this question.

+1 vote

 Since there is no context, a Dockerfile ADD only works if it refers to a remote URL." - https://docs.docker.com/engine/reference/commandline/build/#build-with--

Use -f flag for specifying Dockerfile:

It will definitely resolve your issue.

answered Jun 22, 2018 by Atul
• 10,240 points
+1 vote

I couldn't get COPY to work until I understood the context (I was trying to copy a file from outside of the context)

The docker build command builds an image from a Dockerfile and a context. The build’s context is the files at a specified location PATH. The PATH is a directory on your local filesystem.

A context is processed recursively. So, a PATH includes any subdirectories.

The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon. In most cases, it’s best to start with an empty directory as context and keep your Dockerfile in that directory. Add only the files needed for building the Dockerfile.

Warning: Do not use your root directory, /, as the PATH as it causes the build to transfer the entire contents of your hard drive to the Docker daemon.

answered May 5 by Robert
+1 vote

According to the documentation, these two rules are a must. Keep this in mind

COPY obeys the following rules:

  • The [src] path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the Docker daemon.

  • If [src] is a directory, the entire contents of the directory are copied, including filesystem metadata.

answered May 6 by Jinu
+1 vote
I had the exact same issue. I just moved the Dockerfile at the root level of the docker-compose.yaml file. I'm sure why this workaround works.
answered May 6 by Emilia

Related Questions In Docker

+2 votes
6 answers
0 votes
1 answer

Docker ports are working on the localhost but not with public ip

Probably, the reason is that you've included ...READ MORE

answered Jun 12, 2018 in Docker by Damon Salvatore
• 5,510 points

edited Oct 15, 2018 by Kalgi 289 views
0 votes
1 answer
0 votes
1 answer

What is the difference between “expose” and “publish” in Docker?

Basically, you have three options: Neither specify EXPOSE nor -p -> ...READ MORE

answered Jul 18, 2018 in Docker by Nilesh
• 6,880 points
383 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,900 points
195 views
0 votes
1 answer

Different file owner inside Docker container and in host machine

Here is what you can try. Since ...READ MORE

answered Jun 27, 2018 in Docker by Atul
• 10,240 points
227 views
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,110 points

edited Oct 16, 2018 by lina 4,885 views