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,980 points
22,288 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 6, 2019 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, 2019 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, 2019 by Emilia

Related Questions In Docker

0 votes
1 answer

Yum is not working inside docker container!

Hi@akhtar, Sometime dns is not able to resolve ...READ MORE

answered Apr 22, 2020 in Docker by MD
• 95,460 points
6,266 views
+2 votes
6 answers

copy directories in docker container excluding any one directory

One of the way could be copy ...READ MORE

answered Dec 10, 2018 in Docker by Prateek
34,714 views
+2 votes
1 answer
+2 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
• 58,100 points
2,687 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
1,922 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,220 points

edited Oct 16, 2018 by lina 34,056 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP