Dockerfile can't copy files. ("cp" command)

My Dockerfile contains some cp linux commands.
Because some files in project are decalred as sample such as xxx.php.sample, and it should be replaced as xxx.php.

I thought this process should be executed on Dockerfile. So i wrote like this.

Dockerfile-php-apache

FROM php:5.5-apache RUN apt-get update && \ docker-php-ext-install pdo_mysql mysqli mbstring RUN a2enmod rewrite RUN a2enmod substitute ADD . /var/www WORKDIR /var/www RUN cp config.php.sample config.php RUN cp shared_config.php.sample shared_config.php RUN cp vendor/propel/propel1/build.properties.sample vendor/propel/propel1/build.properties RUN sed -i -E "s/host=(.*);/host=mysql-db;/" ./vendor/propel/propel1/build.properties COPY docker-php-apache/php.ini /usr/local/etc/php/ 

docker-compose.yml

version: '2' services: mysql-db: build: context: . dockerfile: Dockerfile-mysql-server ports: - "3306:3306" php-apache: build: context: . dockerfile: Dockerfile-php-apache volumes: - .:/var/www ports: - "80:80" depends_on: - mysql-db 

After docker-compose build and docker-compose up (No error happened), here is the cp results.

RUN cp config.php.sample config.php -> config.php is not found
RUN cp shared_config.php.sample shared_config.php -> shared_config.php is not found
RUN cp vendor/propel/propel1/build.properties.sample vendor/propel/propel1/build.properties -> build.properties is not found

I thought the path was wrong, so i copied the above commands and executed it directly. (I could login the container by docker exec -it xxx_php-apache_1 bash.)
The cp command finely worked and i could find the copied file in the container and my local source.

Does anyone figure out why cp command on Dockerfile doesn't work?


Here is the corresponds build log.

Step 5 : COPY config.php.sample /var/www/config.php ---> afa7e6139049 Removing intermediate container 38b9968ac061 Step 6 : COPY shared_config.php.sample /var/www/shared_config.php ---> 78ea51045d4e Removing intermediate container fe0b382914bd Step 7 : COPY vendor/propel/propel1/build.properties.sample /var/www/vendor/propel/propel1/build.properties ---> 34c186a193c0 Removing intermediate container c55bb9565035 Step 8 : COPY docker-php-apache/php.ini /usr/local/etc/php/ ---> 4e3e92aa0f97 Removing intermediate container f2c424eb6c38 Successfully built 4e3e92aa0f97 

Just in case, I executed find command on container.

root@d98556ed051a:/var/www# find / -iname '*shared_config.php*' /var/www/shared_config.php.sample 

In addition, I tested with another laptop, but the result was same. These are tested PCs.

  • Mac mini (Mid 2011) OS X Yosemite
  • Mac book pro (Late 2011) OS X Sierra

Each PC is using Docker for Mac Version 1.12.1 (build: 12133)

2 Answers

Approach #1
I believe that the below statements of the Dockerfile can be changed
From:

ADD . /var/www WORKDIR /var/www RUN cp config.php.sample config.php RUN cp shared_config.php.sample shared_config.php RUN cp vendor/propel/propel1/build.properties.sample vendor/propel/propel1/build.properties 

To:

ADD config.php.sample /var/www/config.php ADD shared_config.php.sample /var/www/shared_config.php ADD vendor/propel/propel1/build.properties.sample /var/www/vendor/propel/propel1/build.properties 

Of course, you also use COPY instead of ADD as well in the above.

Approach #2
Change below statements from:

RUN cp config.php.sample config.php RUN cp shared_config.php.sample shared_config.php RUN cp vendor/propel/propel1/build.properties.sample vendor/propel/propel1/build.properties 

To:

RUN ["cp", "config.php.sample", "config.php"] RUN ["cp", "shared_config.php.sample", "shared_config.php"] RUN ["cp", "vendor/propel/propel1/build.properties.sample", "vendor/propel/propel1/build.properties"] 

For more details, refer documentation.

Hope this is helpful.

5

For some reason, it appears that the cp command run through your Dockerfile's RUN instruction doesn't expect the second argument to be the destination file name.

Consider the following:

RUN cp config.php.sample config.php

config.php is not found

It should not try to find config.php, but create it.


I reckon you'd probably be better off using:

RUN mv config.php.sample config.php

That will also delete your (now useless) config.php.sample file. Well, it won't delete nor create anything, simply rename your file -- which is exactly what you want :)

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like