Deleted folder keeps coming back despite being removed

I have the following 2 Dockerfile files:

  • version A
FROM php:8.4.14-fpm-alpine3.22
RUN rm -fr /var/www/html/
RUN mkdir /var/www/test
RUN mv ${PHP_INI_DIR}/php.ini-production ${PHP_INI_DIR}/php.ini
WORKDIR /var/www/test
  • version B
FROM php:8.4.14-fpm-alpine3.22
RUN mkdir /var/www/test
RUN mv ${PHP_INI_DIR}/php.ini-production ${PHP_INI_DIR}/php.ini
RUN rm -fr /var/www/html/
WORKDIR /var/www/test

With version B the directory /var/www/html is actually removed, instead in version A the directory keeps coming back.

I thought it could be that in the base image the /var/www/html was defined as VOLUME, but that’s not the case.

I thought it could be a cache layer problem but also running docker system prune and then docker image build --no-cache doesn’t solve the issue.

Since you didn’t change the WORKDIR before removing the folder, the next time a temporary container starts for the new RUN instruction, it will be created again, since the command still have to run in the working directory.

If you move the WORKDIR definition right after the FROM instruction’s line, you will not even need to create the test folder, because it will be created automatically.

2 Likes