php - docker-compose build and docker build giving different resultsGet the solution ↓↓↓
I have created a simple Dockerfile to install apache with PHP and then install packages from composer.json.
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:
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:
docker-compose is building the image all the console outputs are identical to that of
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:
Alternatively, remove the
volumes directive from your
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.
Share solution ↓
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.