Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using COPY on dockerfile for apache build

Tags:

I'm new to docker and I'm trying to build a small dockefile for apache just for testing.

FROM httpd:2.2.31

RUN mkdir -p /opt/mw/apache-test/logs

ADD ./httpd-custom.conf /usr/local/apache2/conf/httpd.conf
COPY /opt/mw/apache-2.2.31-instance1/htdocs/ /usr/local/apache2/htdocs/

But for some reason I getting an error with the COPY. Also, I tried to use ADD.
This is the error I got:

Step 4 : COPY /opt/mw/apache-2.2.31-instance1/htdocs/ /usr/local/apache2/htdocs/
lstat opt/mw/apache-2.2.31-instance1/htdocs/: no such file or directory  

What I'm doing wrong?

like image 271
radicaled Avatar asked Oct 29 '16 00:10

radicaled


People also ask

Should you use the same Dockerfile for Dev staging and production builds?

Multiple DockerfilesStaging and production should be using the same image, built from the same Dockerfile to guarantee that they are as-similar-as-possible.

Should I use add or COPY Dockerfile?

According to the Dockerfile best practices guide, we should always prefer COPY over ADD unless we specifically need one of the two additional features of ADD. As noted above, using ADD to copy remote files into a Docker image creates an extra layer and increases the file size.


1 Answers

Understand that you do not want to mount the volumes and instead wanted have those files part of the image so that it can be shared. At least assuming this.

As per the Docker documentation

COPY obeys the following rules:
- The 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 is a directory, the entire contents of the directory are copied, including filesystem metadata.

You might got the the problem by now. In your Dockerfile i.e., COPY statement is the problem since it is referring to absolute path which is not following the 1st rule from the above. So, htdocs should be available in the local directory from which you execute the docker build command.

The following changes need to made before building the image:

  • Hope you might have already created a directory(which you are building image ) and this directory contains Dockerfile, httpd-custom.conf files.
  • Now, go into above directory and copy /opt/mw/apache-2.2.31-instance1/htdocs to current directory. So, that htdocs directory can be now part of context ( as mentioned in the docs) while building the image.
  • Change the contents of Dockerfile to the following(mainly COPY command):
FROM httpd:2.2.31

RUN mkdir -p /opt/mw/apache-test/logs

ADD ./httpd-custom.conf /usr/local/apache2/conf/httpd.conf
COPY htdocs /usr/local/apache2/htdocs

Now you should be able to build it successfully.

For just demo, used a light weight busybox and create a directory in the same context (to simulate your case) and it does as you see below:

$ more Dockerfile 
FROM busybox
COPY data_folder  /opt/data_folder
CMD ["ls", "/opt/data_folder"]

$ ls
data_folder  Dockerfile
$ ls data_folder/
test.txt

Build Image:

$ sudo docker build  -t dirtest .
Sending build context to Docker daemon 3.584 kB
Step 1 : FROM busybox
 ---> e02e811dd08f
Step 2 : COPY data_folder /opt/data_folder
 ---> b6b1a9555825
Removing intermediate container b682e0467803
Step 3 : CMD ls /opt/data_folder
 ---> Running in 3b05f08ceafc
 ---> b73190fc1fd9
Removing intermediate container 3b05f08ceafc
Successfully built b73190fc1fd9

Running above Image in a Container which shows directory 'data_folder' is copied and showing its contents. In your case, it is htdocs

$ sudo docker run -it --rm --name testdirtest dirtest
test.txt
like image 176
Rao Avatar answered Sep 23 '22 16:09

Rao