Docker copy composer vendor from inside container - php

I've made a container which I am building through docker-compose, here is .yml:
gateway:
build: .
image: sng
container_name: sng
command: tail -F /dev/null
ports:
- "10091:10091"
volumes:
- vendor:/root/vendor
volumes:
vendor:
And Dockerfile is simply pulling the image and running composer install:
FROM php:7-fpm-alpine
EXPOSE 10091
WORKDIR /root
COPY . .
RUN apk add composer && composer install --no-progress
Is there a way for me to sync back the created vendor folder automatically back to the codebase without running a docker cp from the host machine?

Oh man, im so sorry!! I did what you want, is just mount a volume of vendor to the vendor folder.
volumes:
- path/to/local/vendor:vendor

Related

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,

docker build error while copying source file to php container

I'm trying to set up my symfony project in docker, and i'M struggling building the php container.
I've put all my source code in \var\www\app\
Here is an extract of my docker-compose.yml:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
volumes:
- '/var/www/app:/var/www'
restart: on-failure
env_file:
- .env
user: 1000:1000
nginx:
image: nginx:1.19.0-alpine
restart: on-failure
volumes:
- '/var/www/app:/var/www'
- './docker/nginx/app.conf:/etc/nginx/conf.d/app.conf:ro'
ports:
- '80:80'
depends_on:
- php
here's the Dockerfile:
FROM composer:2.0 as composer
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-enable apcu
WORKDIR /var/www
COPY --chown=1000:1000 var/www/app /var/www
RUN PATH=$PATH:/usr/src/app/vendor/bin:bin
RUN composer install --no-scripts --prefer-dist \
&& rm -rf "$(composer config cache-dir)" "$(composer config data-dir)"
and here's the error I get while trying to build the php container:
Step 9/11 : COPY --chown=1000:1000 var/www/app /var/www
ERROR: Service 'php' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder883740312/var/www/app: no such file or directory
The problem, I guess, is that it tries to fetch file within the docker build folder instead of /var/www/app. I thought it was because of the context, and I've tried to change it but then it cannot find my Dockerfile anymore.
I don't see how to resolve this, yyet I have the feeling it is a veryy easy one...But I'm quite lost at the moment.
Thanks!
You try to COPY a folder at build time and then at runtime you replace the same by using a VOLUME in your docker-compose. Maybe the volume is not needed?
Regarding the error, docker expects your var folder to be at the same level as docker-compose.yml. If that is not the case and your var folder is in docker/php/, then that is what you have to set as context in your docker-compose.yml.
php:
build:
context: docker/php
dockerfile: Dockerfile
If the Dockerfile is in the context, you don't even need to declare it explicitly in the docker-compose.yml

Docker: docker-compose copy files from container to host

I want to copy all files during the installation process in docker-compose.yml file.
If I run:
$ git clone https://github.com/laravel/laravel.git laravel-app
$ cd laravel-app
$ docker run --rm -v $(pwd):/app composer install
It will copy all new files from container to host during the installation process in the docker container.
So I will see new vendor folder and composer.lock file in my laravel-app directory after installation.
But if I setup volume in docker-compose.yml:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
And then setup installation process in Dockerfile:
FROM php:7.4.4-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy existing application directory contents
COPY . /var/www
# Install composer.json dependencies
RUN composer install # <<-- !this one!
It will not copy vendor folder and composer.lock file back to my host.
Hot to make it happen?
how about add composer docker image to your docker compose, and run command install
version: '3'
services:
#PHP Service
#your php image
# composer
composer:
image: composer/composer
volumes:
- ./:/var/www
command: install
You copy all build context into your image /var/www
You run composer install from the image, so vendor and composer.lock only exist in the image, not on your host
You bind mount your current directory(which has composer.json but not vendor and composer.lock) to /var/www, so it replaces the image /var/www
You know your issue, the fix depends on what you want to do exactly, tell me in a comment.
Tip: You should use Docker multi-stage builds to install Composer into your image. It's much cleaner than the curl
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/
RUN composer install --no-scripts --no-suggest --optimize-autoloader

Composer while 'docker-compose' with mount bind

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"

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?

Categories