Composer while 'docker-compose' with mount bind - php

I try to build a docker-compose project for a php application beside a database, which is automatically deployed on a developer machine. It's not a problem, but where it's stuck is the composer-install.
I want to have a directory from the host computer which is bind into the container, so developer are able to change code and are able to see it immediately in the docker instance. That means
Deploy a container with PHP and a webserver
Deploy the database
Bind the local source directory into the PHP container
Execute a composer-install in that directory
The directory tree is like
/
-php
-- src
-- Dockerfile
-postgres
-- Dockerfile
docker-compose.yml
I attached snippets from my docker-compose.yml and the PHP Dockerfile, anyone has an idea why it fails or see the problem in the order or something else or even can me explain what I have to notice? That would be great!
Dockerfile:
FROM xy
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
&& ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
COPY ./src /var/www/html
WORKDIR /var/www/html/
RUN composer install --prefer-source --no-interaction
Docker-compose file:
version: '3.4'
services:
php:
build: ./php
container_name: "php"
volumes:
- ./php/src:/var/www/html
postgres:
image: postgres:10.4-alpine
container_name: "postgres"

Related

Docker install and use composer with laravel and dependencies

Im startig my journal with docker.
I made docker-compose.yml that starts following services:
nginx
PHP 8.1
I setup site to display and read php files, everything is ok. But right now I don't know whats next. I want to install laravel with composer and NPM. How to run it together in that way I can user "composer install", "composer update" in every project.
This is my docker-compose.yml:
services:
nginx:
container_name: nginx_tst
image: nginx:latest
networks:
- php
ports:
- "8080:80"
volumes:
- "./nginx-conf:/etc/nginx/conf.d"
- "./:/usr/share/nginx/html"
php:
container_name: php_tst
image: php:8.1-fpm
networks:
- php
volumes:
- "./:/usr/share/nginx/html"
working_dir: /
networks:
php:
Edit:
I Switched to Laravel Sail, it makes everything by itself
Add command attribute to the php service. Using that you can execute compose install and update commands and etc...
Follow this link to know how to execute multiple commands
Docker Compose - How to execute multiple commands?
You can use something like this.
command: curl -s https://laravel.build/example-app | bash
You can go inside the container
docker exec -it php_tst bash
and then install & run composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
php composer.phar install
install & run nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
nvm install node
node -v
npm i
npm run dev

composer install not working php-alpine docker

I am using php FPM alpine container with nginx alpine container for my laravel application.
Here is my Dockerfile,
FROM php:7.4-fpm-alpine
WORKDIR /usr/src/app
# install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"
# copy files
COPY ./ /usr/src/app/
# install packages
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer install
Here is my docker-compose file,
version: "3.7"
services:
web:
image: nginx:1.19-alpine
ports:
- 80:80
volumes:
- ../api:/usr/src/app
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- api
api:
build: ../api
volumes:
- ../api:/usr/src/app
When I run docker-compose up --build the container build process works as you can see in the attached screenshot but the vendor directory was never created by the composer!?
Is it a known issue while using PHP FPM Alpine containers?
Also, when I access into the container using this command
docker exec -it docker_api_1 /bin/ash
and run composer install manually then everything works. Any suggestions are appreciated.
This is the error message when i visit the site. It can't find the composer autoload.php file,

Why is my volume not copying over to its docker container?

I am trying to run a webserver container with nginx and a php-fpm server running a Laravel application. The directory houses a frontend and backend directory so my paths are not off in the configuration files.
I setup a docker-compose.yml file
version: '2'
services:
webserver:
build:
context: ./
dockerfile: webserver.docker
volumes:
- /home/colesam/Documents/code/todo/backend:/var/www
ports:
- "8080:80"
links:
- backend
backend:
build:
context: ./
dockerfile: backend.docker
volumes:
- ./home/colesam/Documents/code/todo/backend:/var/www
And php-fpm Dockerfile (backend.docker)
FROM php:7.2-fpm
RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN pecl install mcrypt-1.0.2
RUN docker-php-ext-enable mcrypt
RUN docker-php-ext-install pdo mbstring
RUN apt-get install -y apt-transport-https
RUN apt-get install -y curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN chown -R www-data:www-data /var/www
VOLUME ["/var/www"]
RUN ls -al /var/www
WORKDIR /var/www
RUN composer install
And the nginx webserver Dockerfile (webserver.docker)
FROM nginx:1.10
ADD ./vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www
For some reason when I run docker-compose up -d --build I always fail at this step of the build process:
Step 13/13 : RUN composer install
---> Running in 65bb97f03004
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer could not find a composer.json file in /var/www
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
ERROR: Service 'backend' failed to build: The command '/bin/sh -c composer install' returned a non-zero code: 1
From the last line of the output it looks like it failed because /var/www/was empty and didn't have the files copied over like composer.json. I looked around a lot and this stackoverflow issue seemed to be the most related to what was happening but I think I'm already following everything the accepted answer suggests.
Do I need to include a COPY or ADD command even though I'm mounting the folder through a volume?
The build phase does not see any mounted volumes. The only thing that is available to "build" are the things defined in the backend.docker and in the compose section of build:
build:
context: ./
dockerfile: backend.docker
Do I need to include a COPY or ADD command even though I'm mounting the folder through a volume?
Yes, modify your backend.docker similar to this
...
WORKDIR /var/www
ADD composer.json .
RUN composer install

Composer install with dockerfile

I'm pretty new with docker, I try to automatically execute composer install within my Dockerfile but it seems that I can't cd into my application while installing, what's wrong? Or maybe there is another better way to do that?
my docker-compose.yml
version: "3.1"
services:
app:
image: nginx:alpine
container_name: app
working_dir: /application
volumes:
- ./Projects/app:/application/app
- ./Docker/nginx/app.conf:/etc/nginx/conf.d/app.conf
ports:
- "8080:8080"
php-fpm-app:
build: Docker/php-fpm-app
container_name: php-fpm-app
working_dir: /application
volumes:
- ./Projects:/application
- ./Docker/php-fpm-app/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
my Dockerfile
FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
RUN mkdir -p /var/www/.composer \
&& chown -R www-data:www-data /var/www/.composer
USER www-data
RUN cd /application/app; composer install
The output after I run this command:
docker-compose up -d
Step 6/6 : RUN cd /application/app; composer install
---> Running in ac53e653af46
/bin/sh: 1: cd: can't cd to /application/app
Composer could not find a composer.json file in /application
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
ERROR: Service 'php-fpm-app' failed to build: The command '/bin/sh -c cd /application/app; composer install' returned a non-zero code: 1
If I try to remove the last line from my Dockerfile, once it's up and running if I run this command:
docker-compose exec --user www-data php-fpm-app bash -c 'cd
/application/app && composer install'
It works, I don't understand why I can't do this with my Dockerfile.
===========================
Finally
I find a way to execute a script but I don't see the output, so if the script last for many secondes/minutes I won't know when it's done.
ADD ./setup.sh /setup.sh
RUN chmod +x /setup.sh
CMD ["sh", "/setup.sh"]
I decided to execute this script manually once all it's up and running
sh ./setup.sh
You should not run composer install in Dockerfile. It will fail because you have not created/synced volume from the local to container yet. Thus all files including composer.json is not placed under /var/www/html.
What you can do is to add 1 line (command) into your docker-compose.yaml file inside the service eg.
services:
api-php-fpm:
build: ./docker/php-fpm
command: sh -c "composer install"
This is because the volumes aren't mounted until the build is complete. Just use COPY to copy only the composer.json into the folder, and run the composer install as normal.
Referencing the Documentation for Dockerfiles you need to edit your Dockerfile.
it should be:
WORKDIR /application/app
RUN composer install
Does this fix your problem?

How to use php & nodejs from separate containers

Let's say I have the following situation:
docker-compose.yml
version: '3'
services:
web:
build: .
links:
- apache
- php
- node
depends_on:
- apache
- php
- node
volumes:
- .:/var/www/html
apache:
image: httpd
php:
image: php
node:
image: node
and I also have a Dockerfile
FROM phusion/baseimage
RUN apt-get update
RUN apt-get install -yq git curl zip wget curl
RUN curl -s https://getcomposer.org/installer | php # error, no PHP exec found
RUN mv composer.phar /usr/local/bin/composer
RUN npm install -g bower gulp
COPY ./ /var/www/html
WORKDIR /var/www/html
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
CMD ["/sbin/my_init"]
Now my question is: how could I use PHP & Node, which are installed in separate containers, in this main app Dockerfile? Is that even possible, or do I need to manually install PHP & Node inside my Dockerfile?
Docker doesn't work the way you are thinking. This isn't like code inheritance or configuring a single machine with multiple runtime languages. You are configuring multiple virtual machines, each with their own runtime environment.
if you want to run a PHP app, you put that app in the container that has PHP. same with Node.

Categories