Pei-turn
Pei-turn

Reputation: 80

Problems with composer when creating docker image from laravel project

Im new to docker but i have read som guides and tried to create a docker image from my laravel project.

The docker command im running is

sudo docker build -t docker-image .

and this is my Dockerfile:

FROM composer:1.8.5 as build
WORKDIR /app
COPY . /app
RUN composer install

FROM php:7.3-apache
EXPOSE 80
COPY --from=build /app /app
COPY vhost.conf /etc/apache2/sites-available/000-default.conf
RUN chown -R www-data:www-data /app && a2enmod rewrite

The error comes during step 4/9 Run composer install:

[ErrorException]
file_put_contents(/app/vendor/bin/generate-defuse-key): failed to open stream: No such file or directory

the file exists in /vendor/bin/

can anyone tell me what i am doing wrong?

Upvotes: 2

Views: 1404

Answers (2)

Vikash Pathak
Vikash Pathak

Reputation: 3562

If you would like to run your laravel application with nginx you can use the following docker settings.

Here I supposed your application at: /var/www/laravelapp

So the file coposition would be as follow:

1. Your docker-compose file: /var/www/laravelapp/docker-compose.yml

version: "3.1"
services:
  webserver:
    image: nginx:alpine
    restart: always
    container_name: laravel-webserver
    working_dir: /application
    volumes:
        - /var/www/laravelapp:/application
        - /var/www/laravelapp/docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "82:80"
    env_file:
      - .env
    networks: 
      - intranet

  php-fpm:
    build: docker/php-fpm
    restart: always
    container_name: laravel-fpm
    working_dir: /application
    volumes:
      - /var/www/laravelapp:/application
    env_file:
      - .env
    networks: 
      - intranet

networks:
  intranet:
    external: false

2. Nginx dockerfile /var/www/laravelapp/docker/Dockerfile

FROM phpdockerio/php71-fpm:latest

# Install selected extensions and other stuff
RUN apt-get update \
    && apt-get -y --no-install-recommends install  php7.1-mysql php7.1-mbstring php7.1-gd git \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*


WORKDIR "/application"

3. Nginx host settings /var/www/laravelapp/docker/nginx/nginx.conf


server {
    listen 80 default;

    client_max_body_size 108M;

    access_log /var/log/nginx/application.access.log;


    root /application/public;
    index index.php;

    if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
    }

    location ~ \.php$ {
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
    }
    
}

Let me know if still you getting any issue.

Upvotes: 1

Pei-turn
Pei-turn

Reputation: 80

I found the problem it was not a docker, composer or laravel problem but a git problem. the problem was that git could not create a symlink and i missed the error when checking out the project. i found it when i did all the steps again.

Upvotes: 0

Related Questions