0

I'm using docker-engine 17.05.0~ce-0~ubuntu-xenial to build this Dockerfile:

FROM wordpress:apache as codebase

FROM wordpress:cli as cli 
COPY --from=codebase /usr/src/wordpress /var/www/html

# Create a config 
RUN wp --path=/var/www/html config create --dbname=dbname --dbuser=dbuser --skip-check \
 && ls /var/www/html/wp-config.php

RUN ls /var/www/html/wp-config.php

But the output is not as expected. The code I copy from wordpress:apache is correctly added to /var/www/html, but the wp-config.php is only present during the actual build step, but not in the resulting image. I don't understand what I'm doing wrong here. When building, it ends like this:

$ docker build -t wptest . --no-cache
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM wordpress:apache as codebase
 ---> 1d3cc82944da
Step 2/5 : FROM wordpress:cli as cli
 ---> ee57985dea6f
Step 3/5 : COPY --from=codebase /usr/src/wordpress /var/www/html
 ---> e10d62f3d7bc
Removing intermediate container e09a35e05108
Step 4/5 : RUN wp --path=/var/www/html config create --dbname=dbname --dbuser=dbuser --skip-check  && ls /var/www/html/wp-config.php
 ---> Running in 8b2b3e98163e
Success: Generated 'wp-config.php' file.
/var/www/html/wp-config.php
 ---> b0934cfed497
Removing intermediate container 8b2b3e98163e
Step 5/5 : RUN ls /var/www/html/wp-config.php
 ---> Running in cda6a5c16fb5
ls: /var/www/html/wp-config.php: No such file or directory
The command '/bin/sh -c ls /var/www/html/wp-config.php' returned a non-zero code: 1

Why is the wp-config.php that's created in the first RUN not available in the next step, while the data I put there with the COPY command stays there?

(In case you're wondering what I'm trying to achieve, this is just a MCVE.)

Tim Stoop
  • 346
  • 1
  • 12

1 Answers1

1

The wordpress:cli Dockerfile declares VOLUME /var/www/html. Once that VOLUME directive has run, the content of that directory tree is fixed forever more; your COPY and RUN statements that try to add things into that directory have no effect.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • The copy has an effect. If I run the image, there are files in /var/www/html, it's just missing the wp-config. If I run the wordpress:cli image, /var/www/html is empty. – Tim Stoop Jul 26 '18 at 15:10
  • 1
    @TimStoop copy works because it operates directly on the image filesystem layers. The run command does not because it runs in a temporary container, and that temporary container gets an anonymous volume mounted over the image filesystem, where your changes go and get discarded. – BMitch Jul 26 '18 at 15:27
  • Makes sense, thank you! ... Should I mark this answer as the correct one? I can't mark a comment as such :/ – Tim Stoop Jul 26 '18 at 15:35
  • @TimStoop David got you 90% there, give him the credit. – BMitch Jul 26 '18 at 18:14