Docker Compose for Nginx - PHP7.4 fpm - Composer

PHOTO EMBED

Tue Nov 16 2021 11:37:21 GMT+0000 (Coordinated Universal Time)

Saved by @swina #docker #nginx #php #phpfm

## Dockerfile
FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

## EOF Dockerfile

## docker-compose.yaml
# Docker compose for PHP, Composer and nginx
# Check Dockerfile to create app image, PHP 7.4 and Composer
# Run: docker-compose up
# After mounted, open container cli 
# 
# Update laravel packages:
##### composer update
# Restart container to apply nginx configuration
##### docker compose restart 

version: "3.7"
services:
  app:
    build:
      args:
        user: sammy
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: my-app
    container_name: myapp-php
    restart: always
    working_dir: /var/www/
    volumes:
      - ./:/var/www
    networks:
      - webdev

  nginx:
    image: nginx:alpine
    container_name: myapp-nginx
    restart: unless-stopped
    ports:
      - 8000:80
    volumes:
      - ./:/var/www
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    networks:
      - webdev

networks:
  webdev:
    driver: bridge
    
## EOF docker-compose.yml

## create a nginx.conf file in the root of your app
## nginx.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

## EOF nginx.conf


Run: 
docker-compose up

When container is up open container console (cli)
composer update
docker compose restart

content_copyCOPY

A docker compose to run PHP7.4 , Composer and NGINX