Monday, 30 May 2022

So you want to make your own docker-compose for PHP/NPM ?


So you want to make your own docker-compose for PHP and NPM ? I know you do. But you don't want any more headaches, do you? Well, then, this tutorial is for you.


 

 

 

Project Structure:

/docker

/src

docker-compose.yml

.env


 

Make a php.dockerfile:

FROM php:8.1.1-fpm

WORKDIR /var/www/html

RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libwebp-dev \
libxpm-dev \
libgif-dev \
libldap2-dev \
libgmp-dev \
libzip-dev \
libcurl3-dev \
netcat \
&& docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp -with-xpm \
&& docker-php-ext-install -j$(nproc) gd

RUN docker-php-ext-install pdo pdo_mysql zip gmp exif curl

RUN chown -R www-data:www-data /var/www

RUN php -v


RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
#RUN cd /var/www/html/
#RUN composer update -o
#
#RUN php artisan optimize
#COPY /nginx/crontab /etc/crontabs/root
#RUN pwd
#RUN ls
#RUN chmod 777 -R storage
#RUN chmod 777 -R bootstrap/cache/
# RUN set -x ; \
# addgroup -g 82 -S www-data ; \
# adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
# RUN chown -R www-data:www-data /var/www

Make a npm.dockerfile

FROM node:13.7

WORKDIR /var/www/html


RUN npm install
RUN npm run production

Make a docker-compose.yml

version: '3.1'

networks:
fantise:
driver: bridge
volumes:
docker:
driver: local
meilisearch:
driver: local
mysql:
driver: local
src:
driver: local
services:
site:
image: nginx:stable-alpine
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- ./src:/var/www/html
- ./docker/cofigs/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/cofigs/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- php
- redis
- meilisearch
- mysql
networks:
- site
php:
build:
context: .
dockerfile: docker/php.dockerfile
container_name: fantise_php
volumes:
- ./src:/var/www/html
- ./docker/cofigs/php-prod.ini:/usr/local/etc/php/conf.d/php-prod.ini
networks:
- site
composer:
build:
context: .
dockerfile: docker/artisan.dockerfile
container_name: fantise_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: [ 'composer' ]
networks:
- fantise
npm:
image: node:latest
volumes:
- ./src:/var/www/html
- ./docker/logs:/root/.npm/_logs/
working_dir: /var/www/html
entrypoint: ['npm']
artisan:
build:
context: .
dockerfile: docker/php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
links:
- mysql
entrypoint: ['php', '/var/www/html/artisan']
networks:
- site
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- ./mysql:/var/lib/mysql
networks:
- site
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- './redis:/data'
networks:
- fantise
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- ./meilisearch:/data.ms
networks:
- site
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"]
retries: 3
timeout: 5s

No comments:

Post a Comment