how to use composer with docker-compose
If you look at composer/composer:php7
Dockerfile
, then you will see, that it is based on php:7.0-alpine
and it doesn't seem like fpm
is included. So, you could use composer/composer:php7
as base image to install php-fpm
on top of it.
So, since you do the mapping of your project in all three containers, running composer install
in one container should result in the changes be visible in all three containers.
Me personally, I do not see a point in segregating PHP and nginx into 2 different containers, because one is highly dependable on another. And mapping your app into both containers is also a perfect example of nonsense. That's why I maintain my own public build of nginx+php Docker image. You can check it out here. There are more builds with more flavors. And they all come with composer inside.
I set up my docker-compose.yml
file so one docker instance would use the composer/composer
image and execute composer install
within a shared container. All of the other images would then be able to access the vendor directory that composer created. The tricky part was realizing that the composer/composer
image assumes that the composer.json
file will be in an /app
directory. I had to override this behavior by specifying my shared container as the working_dir
instead:
version: '3'services: #=====================# # nginx proxy service # #=====================# nginx_proxy: image: nginx:alpine networks: - test_network ports: - "80:80" - "443:443" volumes: # self-signed testing wildcard ssl certificate - "./certs:/certs" # proxy needs access to static files - "./site1/public:/site1/public" - "./site2/public:/site2/public" # proxy needs nginx configuration files - "./site1/site1.test.conf:/etc/nginx/conf.d/site1.test.conf" - "./site2/site2.test.conf:/etc/nginx/conf.d/site2.test.conf" container_name: nginx_proxy #===============# # composer.test # #===============# composer.test: image: composer/composer networks: - test_network ports: - "9001:9000" volumes: - "./composer:/composer" container_name: composer.test working_dir: /composer command: install #============# # site1.test # #============# site1.test: build: ./site1 networks: - test_network ports: - "9002:9000" environment: - "VIRTUAL_HOST=site1.test" volumes: - "./composer:/composer" - "./site1:/site1" container_name: site1.test #============# # site2.test # #============# site2.test: build: ./site2 networks: - test_network ports: - "9003:9000" environment: - "VIRTUAL_HOST=site2.test" volumes: - "./composer:/composer" - "./site2:/site2" container_name: site2.test# networksnetworks: test_network:
Here is how the directory structure looks:
certs test.crt test.keycomposer composer.jsonsite1 app public Dockerfile site1.test.confsite2 app public Dockerfile site2.test.confdocker-compose.yml