I have created a simple Dockerfile to install apache with PHP and then install packages from composer.json.
FROM php:7-apache WORKDIR /var/www/html COPY ./src/html/ . COPY composer.json . RUN apt-get update RUN apt-get install -y unzip # Install Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update
When I run docker build -t my-web-server .
followed by docker run -p 8080:80 my-web-server
, everything works fine and the packages install.
But when I use a docker-compose file:
version: "3.9" services: ecp: build: . ports: - "8080:80" volumes: - ./src:/var/www
and perform docker-compose build
followed by docker-compose up
The packages do not install and I just index.php is taken across to the container
my current file structure:
src |-- html |-- index.php composer.json docker-compose.yaml Dockerfile
When docker-compose
is building the image all the console outputs are identical to that of docker build
Advertisement
Answer
Your two approaches are not identical. You are using volumes in your docker compose, and not in your docker call. Your problem lies there.
More specifically, notice that in your docker compose you are mounting your host’s ./src
to your container’s ./var/www
– which is not the giving you the correct structure, since you “shadow” the container’s folder that contains your composer.json
(which was copied to the container at build time).
To avoid such confusion, I suggest that if you want to mount a volume with your compose (which is a good idea for development), then your docker-compose.yml
file should mount the exact same volumes as the COPY
commands in your Dockerfile
. For example:
volumes: - ./src/html:/var/www/html - ./composer.json:/var/www/html/composer.json
Alternatively, remove the volumes
directive from your docker-compose.yml
.
Note that it can be a cause for additional problems and confusion to have a file (in your case composer.json
) copied to a folder in the container, while having the same folder also copied to the container as is. It is best to have the structure on the container mimic the one on the host as closely as possible.