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.
Docker Compose Setup for PHP and NPM
To create a Docker Compose setup for PHP and NPM, follow these steps to streamline your development environment. This guide will help you set up a project structure, create Dockerfiles for PHP and NPM, and configure docker-compose.yml
to manage services like MySQL, Redis, and MeiliSearch. By using Docker, you can ensure consistent environments across different development stages, making it easier to manage dependencies and configurations.
Project Structure
- /docker
- php.dockerfile
- npm.dockerfile
- configs
- conf.d
- default.conf
- nginx.conf
- php-prod.ini
- conf.d
- logs
- /src
- docker-compose.yml
- .env
Services
- site: Uses the Nginx web server to serve the application. It listens on port 80 and depends on PHP, Redis, MeiliSearch, and MySQL services.
- php: Builds a PHP environment using the
php.dockerfile
. It runs PHP-FPM to process PHP files. - composer: Runs Composer commands to manage PHP dependencies. It uses the
artisan.dockerfile
for the build context. - npm: Runs NPM commands to manage Node.js dependencies and build assets.
- artisan: Runs Laravel Artisan commands for various tasks like migrations. It uses the
php.dockerfile
for the build context. - mysql: Uses the MySQL server to manage the application's database. It listens on port 3306 and includes a health check.
- redis: Uses the Redis server for caching and session management. It listens on port 6379 and includes a health check.
- meilisearch: Uses the MeiliSearch server for search functionality. It listens on port 7700 and includes a health check.
Create 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
Create npm.dockerfile
FROM node:13.7
WORKDIR /var/www/html
RUN npm install
RUN npm run production
Create 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/configs/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/configs/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/configs/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
Example .env
File
APP_PORT=8080
WWWUSER=1000
SAIL_XDEBUG_MODE=off
SAIL_XDEBUG_CONFIG=client_host=host.docker.internal
FORWARD_DB_PORT=3306
DB_PASSWORD=secret
DB_DATABASE=mydatabase
DB_USERNAME=myuser
FORWARD_REDIS_PORT=6379
FORWARD_MEILISEARCH_PORT=7700
Docker Compose Commands
- Build and start the containers:
docker-compose up --build
- Start the containers:
docker-compose up
- Stop the containers:
docker-compose down
Run One-Time Commands
- Run Composer:
docker-compose run --rm composer install
- Run Artisan:
docker-compose run --rm artisan migrate
- Run NPM:
docker-compose run --rm npm install
No comments:
Post a Comment