1
votes

I have a Laravel Project and I created a docker-compose and dockerfile.

I do docker-compose build and my problem is that:

  1. It does not create the folder vendor (composer install).
  2. Do not copy .env.local to .env
  3. Do not create the node_modules folder

docker-compose.yml

version: "3"

services:
  api:
    container_name: nadal_api
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html/app
    ports:
      - 5002:80

DockerFile

FROM composer:latest AS composer

#Generamos el primer build desde un container de nodejs
FROM node:latest as nodebuild
COPY --chown=root:www-data . /var/www/html/app
WORKDIR /var/www/html/app
RUN npm install
RUN npm run production

#Tomamos la imagen de php fpm para utilizar las librerias compiladas
FROM php:7.2-fpm-stretch

#Instalamos nginx y otras dependencias del framework
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    wget \
    lsb-release \
    libxml2-dev \
    nginx \
    ca-certificates \
    git \
    zip

#Compilamos pgsql
RUN apt-get install -y libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql

#Compilamos las librerias requeridas por laravel
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install xml
RUN docker-php-ext-install mbstring

#Install supervisor porque debemos tener dos procesos, nginx y fpm
RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list

RUN apt-get update && apt-get install -y \
    supervisor \
    curl \
    libssl-dev \
    libmcrypt-dev

#Copiamos las configuraciones de nginx, el host y supervisor
ADD docker/nginx.conf /etc/nginx/nginx.conf
ADD docker/virtualhost.conf /etc/nginx/conf.d/default.conf
ADD docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

#Copiamos el projecto con los modulos de node y los assets compilados
COPY --from=nodebuild --chown=root:www-data /var/www/html/app /var/www/html/app
COPY --chown=root:www-data ./.env.local /var/www/html/app/.env

#Aplicamos los permisos que corresponden
RUN chmod -R g+w /var/www/html/app/storage
RUN chmod -R g+w /var/www/html/app/bootstrap

#Copiamos composer
COPY --from=composer /usr/bin/composer /usr/bin/composer

##Download composer packages
WORKDIR /var/www/html/app
RUN composer install
RUN chmod -R g+w /var/www/html/app/vendor
RUN php artisan key:generate

EXPOSE 80

CMD ["/usr/bin/supervisord"]

Can it be the permissions?

Step 21/30 : COPY --chown=root:www-data ./.env.${environment:-local} /var/www/html/app/.env ---> 7d1038fe2615

Step 26/30 : RUN composer install ---> Running in 8e3265277308

Removing intermediate container 8e3265277308 ---> 429553e893a4 Step 27/30 :

RUN chmod -R g+w /var/www/html/app/vendor ---> Running in 9e276059478b Removing intermediate container 9e276059478b ---> d52ee5ad4ec4

1
Your volumes: declaration is hiding the interesting work that's happening in the Dockerfile. If you want to see the /var/www/html/app/vendor from the image, you can't mount content over /var/www/html.David Maze
I need to change the path o delete the volume? if I need store data, is possible without volumen?Juan Pablo B
Store the data in a different directory from your application. Your Dockerfile installs two different database drivers (!), so the best case is to store all of the data in a relational database and nothing in the local filesystem.David Maze
General Dockerfile practice is to install only what you need. If your application uses MySQL, then don't install the PostgreSQL driver. Your application doesn't need git to run. Prefer running nginx in a separate container, and then you don't need supervisord.David Maze
Yes, this would then be another container. Every single service you define in your docker-compose.yml will generate an own container. You can define as many services in there as you want and thus reach better isolation between your applications, for example running a webserver (e.g. as reverse proxy) in a separate container is a common thing to do.bellackn

1 Answers

1
votes

When building the image, you COPY your whole current directory to /var/www/html/app, then stuff happens with the content there. On docker-compose up, you mount your whole folder again at this destination, which overwrites everything that happened on docker-compose build. To avoid this, just remove the volumes part from your docker-compose.yml.