Docker Community Forums

Share and learn in the Docker community.

Using docker image build to add a layer

Below is my Dockerfile (my first ever, btw). I am trying to add a layer on the bitnami/apache image that includes my source code and call it “apache-with-code”. As you can see, COPY is failing. I am hoping to copy my source code from /var/www/html/sub_crud on my laptop to /var/www/html/sub_crud in the image. Why is this failing?

richb201@richb201-XPS-13-9370:~/myapp$ docker image build -t apache-with-code .
Sending build context to Docker daemon 2.141MB
Step 1/3 : FROM bitnami/apache:latest
—> 06a4698ab84f
Step 2/3 : WORKDIR /var/www/html/sub_crud
—> Using cache
—> d67a0f7c6fc8
Step 3/3 : COPY /var/www/html/sub_crud/ /var/www/html/sub_crud/
COPY failed: stat /var/lib/docker/tmp/docker-builder949400167/var/www/html/sub_crud: no such file or directory

Well, it is 4AM here so I am trial and erroring a little more :sleeping:. I realized that perhaps I need to create the directory in the image. So I added RUN mkdir -p /var/www/html/sub_crud to the Dockerfile. I am now getting mkdir: cannot create directory var/www : permission denied.

Any ideas how to copy my source code into the image? Perhaps I need to create a volume first?


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.

Note : The directory itself is not copied, just its contents.

Thanks. So it seems that if I copy (on my laptop’s drive) /var/www/html/sub_crud/ directory to ./myapp/* I will then be able to use the Dockerfile I created? It will then be within the context? BTW, is there anyway to do this “laptop copy” within Docker or do I need to do this manually?

The context that applies in your situation: put the folders/files in the folder where your Dockerfile is located. Reference them with a “./” prefix in your COPY operations.

Thanks. The problem is that there are MANY directories in PHP/CI. For example the controllers go in the controllers directory (/var/www/html/sub_crud/application/controllers/) while the CI config files go in the config directory. Both of these are under the sub_crud/application directory. The bottom line is that it is quite complex and Codeigniter expects these files to be in the directories where they are currently located. If I don’t put these in directories that CI expects, there is no way that it would work. Are you saying that all of the app files need to be in a /app single folder? Is there a problem with the path length?

I posted you a link to the documentation, which describe how it needs to be used. I understand you don’t like it or have concerns. Well: start trying and report back your experience, I am quite sure others will find your discoveries valuable as well!

Good luck!

Will do! I specialize in “trial and error”!

1 Like

Well, I put the Dockerfile into the html directory and that seems to be working. I am having a problem with copying the /cache directory which is within the sub_crud structure. These files are left overs from previous runs and are not important to persist. I am working on .dockerignore to not copy those cache files.