Docker volume mounting empty

Viewed 977

I'm making an image to host a PHP application. I'm using COPY to populate /var/www/html with the app files and creating a VOLUME /var/www/html to be able to mount the dir on the host and edit files like config.

But:

  1. When I mount the volume on docker-compose.yml, the directory is empty.

  2. When I omit the "volmue" entry on the docker-compose.yml and connect with the container shell, the directory /var/www/html is filled.

I already read tons of examples and documentations but, sincerely, don't know what is wrong.

dockerfile:

FROM php:8.0-apache-buster
LABEL description="OCOMON 3.2 Frontend (noDB)"
RUN docker-php-ext-install pdo_mysql

WORKDIR /var/www/html/ocomon

COPY ./ocomon .

VOLUME /var/www/html/ocomon

docker-compose.yml:

version: '3.5'
services:
    ocomon:
        image: ocomon:3.2
        container_name: ocomon
        volumes:
            - ./volumes/ocomon/ocomon:/var/www/ocomon
        ports:
            - 4682:80
1 Answers

Assuming your host's directory is ${PWD}/www/html, then you need only provide the volumes value in docker-compose.yml and it should be:

Dockerfile:

FROM php:8.0-apache-buster
LABEL description="OCOMON 3.2 Frontend (noDB)"
RUN docker-php-ext-install pdo_mysql

WORKDIR /var/www/html/ocomon

COPY ./www/html/ocomon .

and: docker-compose.yml:

version: '3.5'
services:
  ocomon:
    image: ocomon:3.2
    container_name: ocomon
    volumes:
    - ${PWD}/www/html:/var/www/html
    ports:
    - 4682:80

Explanation

  • Dockerfile VOLUMES creates a volume in the image. By following WORKDIR (which always create if the path does not exist), with VOLUME, you overwrite whatever was either in (or created by WORKDIR). NOTE this is done during image build.

  • Docker Compose volumes mounts a directory from the host into the image. The syntax is volumes: - ${HOST_PATH}:${IMAGE_PATH}. NOTE this is done during image run.

Related