Search code examples
dockerdocker-composedocker-build

Docker build: mkdir creates folders, but they disappear in runtime


I'm trying to extend an Apache 2 Docker container with the following Dockerfile:

FROM joomla:3-apache-php7    
RUN apt-get update
RUN a2enmod ssl

RUN printf "\nIncludeOptional /data/apache/conf-enabled/*.conf\n" >> /etc/apache2/apache2.conf
RUN printf "include_path = '.:/data/php/conf.d'" > /usr/local/etc/php/conf.d/additional.ini

RUN bash -c 'mkdir -pv /data/{apache/conf-enabled,php/conf.d}'
RUN ln -s /etc/apache2/sites-enabled/ /data/apache/sites-enabled

VOLUME ["/var/www/html/", "/data/apache/", "/data/php/"]

EXPOSE 80 443

ENTRYPOINT ["/entrypoint.sh"]
CMD ["apache2-foreground"]

When I try to start the container, this appears:

root@n06 / # docker-compose up -d
root@n06 / # docker-compose logs container
apache2: Syntax error on line 223 of /etc/apache2/apache2.conf: Could not open config directory /data/apache/conf-enabled: No such file or directory

When I comment out RUN printf "\nIncludeOptional /data/apache/conf-enabled/*.conf\n" >> /etc/apache2/apache2.conf and log into the running container:

root@1c72507b2024:/# ls -la /data/**
/data/apache:
total 8
drwxr-xr-x 2 root root 4096 Sep  3 11:39 .
drwxr-xr-x 5 root root 4096 Sep  3 11:50 ..

/data/php:
total 8
drwxr-xr-x 2 root root 4096 Sep  3 11:39 .
drwxr-xr-x 5 root root 4096 Sep  3 11:50 ..

The created dirs disappear randomly, sometimes the /data/apache/conf-enabled directory is there.

Edit

docker-compose.yml:

version: '2'

networks:
  main:
    driver: bridge
    ipam:
       driver: default
       config:
         - subnet: 172.18.0.0/16
           gateway: 172.18.0.1

services:
  test:
    build: docker/test
    container_name: test
    networks:
      - main
    volumes:
      - /mnt/docker/test/joomla:/var/www/html
      - /mnt/docker/test/apache:/data/apache/
      - /mnt/docker/test/php:/data/php/
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
      - /etc/localtime:/etc/localtime
    links:
      - mariadb:mariadb
    environment:
      - JOOMLA_DB_USER=test
      - JOOMLA_DB_PASSWORD=test
      - JOOMLA_DB_HOST=mariadb
      - JOOMLA_DB_NAME=test

Solution

  • There's not really enough data to answer this with certainty, but the biggest hint comes from the fact that you're defining these as volumes:

    VOLUME ["/var/www/html/", "/data/apache/", "/data/php/"]
    

    I suspect that your compose command is pulling in these folders from previous runs. And if you're running on the external swarm, those volumes may be different depending on what node the swarm runs your container on. To know for sure, we'd need to see your docker-compose.yml, know more about the environment where you're running these commands, and all of the docker-compose commands you are running (stop is very different from down).

    Note that I don't recommend creating a volume as part of your Dockerfile because it restricts any images built on this from being able to modify those folders (any changes they make will usually be lost), and it creates anonymous container volumes when you don't define the volume location in your compose. Those anonymous container volumes need to be cleaned with an extra flag on remove (docker rm -v) or manually with a docker volume rm on each unique id.


    Responding to the edited question: with that compose volume mount, it depends on what's in /mnt/docker/test/apache, the contents of the directory inside the container will be completely overwritten by whatever is mounted from the host. If your container is running on different host (e.g. swarm), each host may have different contents in this location. If the directory is missing, that will result in an empty directory in the container.