Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 342
dosuken123 Avatar asked Oct 31 '16 04:10

dosuken123


People also ask

What is cp in Dockerfile?

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH . You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH , you can also stream a tar archive from STDIN or to STDOUT .

How do I copy a folder using Docker cp?

If you want to copy the /tmp/foo directory from a container to the existing /tmp directory on your host. If you run docker cp in your (home) directory on the local host: $ docker cp compassionate_darwin:tmp/foo /tmp Docker creates a /tmp/foo directory on your host.


1 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.

like image 124
Rao Avatar answered Oct 03 '22 04:10

Rao