docker-compose build and docker build giving different results - php

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

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.

Related

Docker project with php composer, installs composer packages in root directory instead of vendor

I have a php/wordpress project which requires composer. The project setup is simple and minimal.
docker-compose.yaml
version: "3.9"
services:
# Database
clearlaw-mysql1:
image: mysql:8
volumes:
- database:/var/lib/mysql
restart: on-failure
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- clearlaw
# Wordpress
clearlaw-wp1:
container_name: clearlaw-wp1
build:
context: .
depends_on:
- clearlaw-mysql
image: wordpress:latest
ports:
- 10002:80
restart: unless-stopped
CLI_MULTISITE_DEBUG: 1
CLI_MULTISITE_DEBUG_DISPLAY: 1
CLI_MULTISITE_DB_HOST: clarlaw-mysql:3306
CLI_MULTISITE_DB_NAME: wordpress
CLI_MULTISITE_DB_USER: wordpress
CLI_MULTISITE_DB_PASSWORD: wordpress
networks:
- clearlaw
clearlaw-adminer1:
image: adminer
ports:
- 10003:8080
restart: unless-stopped
networks:
- clearlaw
networks:
clearlaw:
volumes:
database:
Dockerfile
FROM wordpress:latest
# INSTALL AND UPDATE COMPOSER
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN composer self-update
COPY composer.json .
RUN composer install --prefer-dist
RUN composer dump-autoload
COPY . .
EXPOSE 80
composer.json
{
"require": {
"vlucas/phpdotenv": "^v2.6.7",
"dompdf/dompdf": "^1.0"
}
}
When I run this setup I get fatal error autoload.php file is not where it should be (/vendor/autoload). instead it is in the root directory along with all the installed pacakges. The vendor directory exists however it is empty.
Example Directory structure:
-- autoload.php
vendor # empty
composer
wp-content
wp-admin
wp-includes
# all other files
What I've Tried?
I have tried adding vendor directory explicitly in composer.json but it didn't help
{
"config": {
"vendor-dir": "vendor"
},
"require": {
"vlucas/phpdotenv": "^v2.6.7",
"dompdf/dompdf": "^1.0"
}
}
Update
I have created this repository for you to quick test https://github.com/prionkor/wp-composer-test
The wordpress:latest container has a volume specified at /var/www/html which is also the containers working directory.
When you bring your docker-compose up, the (anonymous) volume is created and the containers entry-point script (/usr/local/bin/docker-entrypoint.sh) copies the WordPress sources into the new volume.
In general only after that a composer install with the vendor dir at /var/www/html/vendor would not be discarded.
As you have the composer install within the Dockerfile building FROM wordpress:latest the vendor folder is created but then discarded. It is either too early or too late depending on from where you look.
Therefore, you can drop the RUN composer ... instructions from your Dockerfile, these are effectively only lengthen the build time.
FROM wordpress:latest
# INSTALL AND UPDATE COMPOSER
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN composer self-update
COPY . .
EXPOSE 80
Then do the composer install (dump-autoload is not necessary after it) and exec the base containers entrypoint when starting the container:
# WordPress
build:
context: .
command: /bin/bash -c "set -ex; composer update --prefer-dist; exec docker-entrypoint.sh apache2-foreground"
Then Composer installs into the volume, effectively at /var/www/html/vendor.
The original WordPress container setup will give a warning that the folder is not empty any longer, but it does not prevent the initialization of WordPress, so you can ignore it:
WARNING: /var/www/html is not empty! (copying anyhow)
Composer then installs the build contexts composer.json/composer.lock when you bring the project up initially (if the composer.lock file is leading in the project outside the container use composer install instead of composer update).
You then can run
docker-compose up -d --build
to recreate the WordPress service container from scratch and Composer populates the vendor folder again.
Commonly this is the time when you can greatly benefit from a command runner, for example a Makefile. You then have the standard operations in your project at your fingertips.
Here a composer-update goal:
wordpress := clearlaw-wp1
cu: composer-update;
composer-update:
tar -c -f- composer.json \
| docker cp - $(wordpress):/var/www/html
docker-compose exec $(wordpress) composer update
Then alias the make command for that file in your shell (e.g. alias dc="make -C $(pwd) -f Makefile") and then you only need to type dc cu to perform the update.
You can extend it further for other operations, and you can also experiment with the volumes as you have commented them out in your example. E.g. you can mount things in read-only inside the existing volume /var/www/html, just not directly at that place.
You can also mount single files. E.g. to extend on the composer example, you could as well mount the composer.json (and composer.lock) file into the container, then you can spare the tar-pipe to docker cp. Which also serves as an example how you can get files out of the container as it works in both directions.
command: /bin/bash -c "set -x; composer update --prefer-dist; exec docker-entrypoint.sh apache2-foreground"
volumes: ["./composer.json:/var/www/html/composer.json:ro"]
You then can run
docker-compose up -d --build
again to recreate the container and trigger the Composer update.
And a side-note: Take a bit of care with the vlucas/phpdotenv package, it can become easily incompatible with docker / docker-compose if you don't follow the standard syntax rules for dot-env files (shell, docker etc.). Normally you also don't need it for a container setup.

Docker compose volume overrides COPY in multi-stage build

My goal is to get php dependencies in one stage of a docker file then copy those dependencies to the next stage (the vendor/ dir). However, once a volume is specified in docker-compose.yml that overrides the COPY statement as if it never happened.
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: docker/app/Dockerfile
volumes:
- .:/var/www/html
docker/app/Dockerfile
FROM composer AS php_builder
COPY . /app
RUN composer install --no-dev
FROM php:7.1-fpm
COPY --from=php_builder /app/vendor /var/www/html/vendor
The result of building and running this is a /var/www/html directory that doesn't have the vendor/ directory as I'd expect.
My guess is that this is because the volume specified in the docker-compose.yml service definition actually happens after the COPY --from statement which seems to be logical. But how do I get around this? I'd still like to use a volume here instead of an ADD or COPY command.
You can combine bind mounts & volume to make your aim, a minimal example for your reference:
docker-compose.yaml:
version: "3"
services:
app:
build:
context: .
dockerfile: docker/app/Dockerfile
volumes:
- my_folder:/var/www/html/vendor
- .:/var/www/html
volumes:
my_folder:
docker/app/Dockerfile:
FROM composer AS php_builder
COPY . /app
#RUN composer install --no-dev
RUN mkdir -p vendor && echo "I'm dependency!" > vendor/dependencies.txt
FROM php:7.1-fpm
COPY --from=php_builder /app/vendor /var/www/html/vendor
Results:
shubuntu1#shubuntu1:~/test$ ls
docker docker-compose.yaml index.php
shubuntu1#shubuntu1:~/test$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_app_1 ... done
shubuntu1#shubuntu1:~/test$ docker exec -it test_app_1 /bin/bash
root#bf59d8684581:/var/www/html# ls
docker docker-compose.yaml index.php vendor
root#bf59d8684581:/var/www/html# cat vendor/dependencies.txt
I'm dependency!
From above execution, you can see the dependencies.txt which generated in the first stage of Dockerfile still could be seen in the container, volume just manage the data by docker itself, while bind mounts give you chance to manage data for yourself.

docker-compose overrides directories in the container

Context
I set up a PHP application recently to work in a docker container connected to a database in a different container.
In production, we're using a single container environment since it just connects to the database which is hosted somewhere else. Nonetheless, we decided to use two containers and docker-compose locally for the sake of easing the development workflow.
Problem
The issue we've encountered is that the first time we build and run the application via docker-compose up --build Composer's vendor directory isn't available in the container, even though we had a specific RUN composer install line in the Dockerfile. We would have to execute the composer install from within the container once it was running.
Solution found
After a lot of googling around, we figured that we had two possible solutions:
change the default command of our Docker image to the following:
bash -c "composer install && /usr/sbin/apache2ctl -D FOREGROUND"
Or simply override the container's default command to the above via docker-compose's command.
The difference is that if we overrid the command via docker-compose, when deploying the application to our server, it would run seamlessly, as it should, but when changing the default command in the Dockerfile it would suffer a 1 minute-ish downtime everytime we deployed.
This helped during this process:
Running composer install within a Dockerfile
Some (maybe wrong) conclusions
My conclusion was that that minute of downtime was due to the container having to install all the dependencies via composer before running the Apache server, vs simply running the server.
Furthermore, another conclusion I drew from all the poking around was that the reason why the docker-compose up --build wouldn't install the composer dependencies was because we had a volume specified in the docker-compose.yml which overrid the directories in the container.
These helped:
https://stackoverflow.com/a/38817651/4700998
https://stackoverflow.com/a/48589910/4700998
Actual question
I was hoping for somebody to shed some light into all this since I don't really understand what's going on fully – why running docker-compose would not install the PHP dependencies, but including the composer install in the default command would and why adding the composer install to the docker-compose.yml is better. Furthermore, how do volumes come into all this, and is it the real reason for all the hassle.
Our current docker file looks like this:
FROM php:7.1.27-apache-stretch
ENV DEBIAN_FRONTEND=noninteractive
# install some stuff, PHP, Apache, etc.
WORKDIR /srv/app
COPY . .
RUN composer install
RUN service apache2 restart
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
And our current docker-compose.yml like this:
version: '3'
services:
database:
image: mysql:5.7
container_name: container-cool-name
command: mysqld --user=root --sql_mode=""
ports:
- "3306:3306"
volumes:
- ./db_backup.sql:/tmp/db_backup.sql
- ./.docker/import.sh:/tmp/import.sh
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: my_user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: test
app:
build:
context: .
dockerfile: Dockerfile
image: image-name
command: bash -c "composer install && /usr/sbin/apache2ctl -D FOREGROUND"
ports:
- 8080:80
volumes:
- .:/srv/app
links:
- database:db
depends_on:
- database
environment:
DB_HOST: db
DB_PORT: 3306
DB_DATABASE: my_db
DB_USER: my_user
DB_PASSWORD: password
Your first composer install within Dockerfile works fine, and your resulting image has vendor/ etc.
But later you create a container from that image, and that container is executed with whole directory being replaced by a host dir mount:
volumes:
- .:/srv/app
So, your docker image has both your files and installed vendor files, but then you replace project directory with one on your host which does not have vendor files, and final result looks like the building was never done.
My advice would be:
don't add second command build to the Dockerfile
mount individual folders in your container, i.e. not .:/srv/app, but ./src:/srv/app/src, etc.
or, map whole folder, but copy vendor files from image/container to your host
or use some 3rd party utility to solve exactly this problem, e.g. http://docker-sync.io or many others

docker-compose using old volumes

I am trying to set up a CI pipeline with docker-compose and am struggling to understand how named volumes work...
As part of my Dockerfile, I copy in the application files and then run composer install to install the application dependencies. There are some elements of the applicaton files and the dependencies that I want to share with the other containers that are running / are set up to be run to perform utility processes (such as running database migrations). See the example below:
Dockerfile:
FROM php:5.6-apache
# Install dependencies
COPY composer.* /app/
RUN composer install --no-dev
# Copy application files
COPY bin bin
COPY environment.json environment.json
VOLUME /app
docker-compose.yml
web:
build:
context: .
dockerfile: docker/web/Dockerfile
volumes:
- app:/app
- ~/.cache/composer:/composer/cache
migrations:
image: my-image
depends_on:
- web
environment:
- DB_DRIVER=pdo_mysql
- AUTOLOADER=../../../vendor/autoload.php
volumes:
- app:/app
working_dir: /app/vendor/me/my-lib
volumes:
app:
In the example above (irrelevant information omitted), I have a "migrations" service that pulls the migrations from the application dependencies installed with composer. My idea is that when I perform docker-compose build followed by docker-compose up, it will bring up the latest version of software with the latest dependencies and run the latest migrations at the same time.
This works fine the first time. Unfortunately on subsequent runs I cannot get docker-compose to use the new versions. If I run docker-compose build, I can see the composer install run and install all the latest libraries, but then when I go into the container with docker-compose run web /bin/bash, the old dependencies are in there! If I run the image directly with docker run web_1, I can see all the latest files no problem. So it's definitely a compose-specific problem.
I assume I need to do something like clear out the volume cache, but whatever I have tried doesn't seem to work. I can only assume I am misunderstanding the idea of volumes.
Any help would be hugely appreciated. Thanks!
What I understand from your question is you want to run composer install every time you run your container. In that case you have to use CMD instruction to execute that command.
CMD composer install --no-dev
RUN and CMD are both Dockerfile instructions.
RUN lets you execute commands inside of your Docker image. These commands get executed once at build time and get written into your Docker image as a new layer.
For example if you wanted to install a package or create a directory inside of your Docker image then RUN will be what you’ll want to use. For example, RUN mkdir -p /path/to/folder.
CMD lets you define a default command to run when your container starts.
You could say that CMD is a Docker run-time operation, meaning it’s not something that gets executed at build time. It happens when you run an image. A running image is called a container.
The problem here has to do with mounting a volume over a location defined in the build. The first build of the image has composer put its output into /app, and the first run of the first build mounts the app named volume to /app. This clobbers the image version of /app with a new write-layer on top. Mounting this named volume on the second build of the image will keep the original contents of /app.
Instead of using a named volume, use volumes-from to load the exported /app volume from web into the migration container.
version: '2'
services:
web:
build:
context: .
dockerfile: docker/web/Dockerfile
volumes:
- ~/.cache/composer:/composer/cache
migrations:
image: docker-registry.efficio.digital:5043/doctrine-migrator:1.1
depends_on:
- web
environment:
- DB_DRIVER=pdo_mysql
- AUTOLOADER=../../../vendor/autoload.php
volumes_from:
- web:ro

Files changes not reflected in Docker image after rebuild

I'm trying to set up two Docker images for my PHP web application (php-fcm) reversed proxied by NGINX. Ideally I would like all the files of the web application to be copied into the php-fcm based image and exposed as a volume. This way both containers (web and app) can access the files with NGINX serving the static files and php-fcm interpreting the php files.
docker-compose.yml
version: '2'
services:
web:
image: nginx:latest
depends_on:
- app
volumes:
- ./site.conf:/etc/nginx/conf.d/default.conf
volumes_from:
- app
links:
- app
app:
build: .
volumes:
- /app
Dockerfile:
FROM php:fpm
COPY . /app
WORKDIR /app
The above setup works as expected. However, when I make any change to the files and then do
compose up --build
the new files are not picked up in the resulting images. This is despite the following message indicating that the image is indeed being rebuilt:
Building app
Step 1 : FROM php:fpm
---> cb4faea80358
Step 2 : COPY . /app
---> Using cache
---> 660ab4731bec
Step 3 : WORKDIR /app
---> Using cache
---> d5b2e4fa97f2
Successfully built d5b2e4fa97f2
Only removing all the old images does the trick.
Any idea what could cause this?
$ docker --version
Docker version 1.11.2, build b9f10c9
$ docker-compose --version
docker-compose version 1.7.1, build 0a9ab35
The 'volumes_from' option mounts volumes from one container to another. The important word there is container, not image. So when you rebuild an image, the previous container is still running. If you stop and restart that container, or even just stop it, the other containers are still using those old mount points. If you stop, remove the old app container, and start a new one, the old volume mounts will still persist to the now deleted container.
The better way to solve this in your situation is to switch to named volumes and setup a utility container to update this volume.
version: '2'
volumes:
app-data:
driver: local
services:
web:
image: nginx:latest
depends_on:
- app
volumes:
- ./site.conf:/etc/nginx/conf.d/default.conf
- app-data:/app
app:
build: .
volumes:
- app-data:/app
A utility container to update your app-data volume could look something like:
docker run --rm -it \
-v `pwd`/new-app:/source -v app-data:/target \
busybox /bin/sh -c "tar -cC /source . | tar -xC /target"
As BMitch points out, image updates don't automatically filter down into containers. your workflow for updates needs to be revisited. I've just gone through the process of building a container which includes NGINX and PHP-FPM. I've found, for me, that the best way was to include nginx and php in a single container, both managed by supervisord.
I then have scripts in the image that allow you to update your code from a git repo. This makes the whole process really easy.
#Create new container from image
docker run -d --name=your_website -p 80:80 -p 443:443 camw/centos-nginx-php
#git clone to get website code from git
docker exec -ti your_website get https://www.github.com/user/your_repo.git
#restart container so that nginx config changes take effect
docker restart your_website
#Then to update, after committing changes to git, you'll call
docker exec -ti your_website update
#restart container if there are nginx config changes
docker restart your_website
My container can be found at https://hub.docker.com/r/camw/centos-nginx-php/
The dockerfile and associated build files are available at https://github.com/CamW/centos-nginx-php
If you want to give it a try, just fork https://github.com/CamW/centos-nginx-php-demo, change the conf/nginx.conf file as indicated in the readme and include your code.
Doing it this way, you don't need to deal with volumes at all, everything is in your container which I like.

Categories