Search code examples
dockerdocker-multi-stage-build

Docker multi-stage build do not keep result of RUN command


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


Solution

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