Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize setup, upgrade PHP Dockerfile to PHP 8.3, add build args and environment variables #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
APP_NAME=Laravel
APP_NAME=Laravel

# PHP container

## NB: PHP user should match the nginx container user due to permissions
PHP_USER=www
PHP_GROUP=www
PHP_PORT=9000

# MySQL container
MYSQL_USER=laravel
MYSQL_PASSWORD=
MYSQL_DATABASE=laravel
MYSQL_ROOT_PASSWORD=root
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.env
.idea/
.idea/

# Project files should be excluded, this is only Docker setup repository
src
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ For now, if you want to run any commands through appropriate containers, you hav
There's also an option to just go into container with `bash` (or whatever shell the container posesses) and run commands you need, like `docker-compose exec bash` and in container `php artisan migrate`.

## To-do for now
- Command delegator script for running commands within appropriate containers
- Global search/replace from project root .env file so that there doesn't have to be manual search & replace for all the values
- Look to see if something differs for containerizing Symfony projects
- Command delegator script for running commands within appropriate containers
37 changes: 21 additions & 16 deletions config/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
FROM php:8.0-fpm
FROM php:8.3-fpm-alpine

ARG uid 1000
ARG guid 1000

ENV UID ${uid}
ENV GUID ${guid}
ENV USER www-data
ENV GROUP www-data
ENV PORT 9000

# Copy composer.lock and composer.json
COPY ./src/composer.lock ./src/composer.json /var/www/
Expand All @@ -7,7 +16,7 @@ COPY ./src/composer.lock ./src/composer.json /var/www/
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
RUN apk --no-cache add -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
Expand All @@ -26,26 +35,22 @@ RUN apt-get update && apt-get install -y \
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd

# Install composer
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY ./src /var/www
# Add user for Laravel application and set proper UID/GUID
RUN groupadd -g 1000 www && useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory permissions
COPY --chown=www:www ./src /var/www
# Copy existing application directory with proper ownership
COPY --chown=${USER}:${GROUP} ./src /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
# Expose port and start php-fpm server
EXPOSE ${PORT}
CMD ["php-fpm"]
8 changes: 0 additions & 8 deletions config/mysql/init.sql

This file was deleted.

23 changes: 10 additions & 13 deletions docker-compose.yml → docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

version: '3'

services:

#PHP Service
app:
build:
context: .
Expand All @@ -17,11 +16,13 @@ services:
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
USER: ${PHP_USER}
GROUP: ${PHP_GROUP}
PORT: ${PHP_PORT}
working_dir: /var/www
networks:
- app-network

# Web server
nginx:
image: nginx:alpine
container_name: ${APP_NAME}.nginx
Expand All @@ -36,8 +37,7 @@ services:
networks:
- app-network

#MySQL Service
db:
mariadb:
image: mysql:8.0
container_name: ${APP_NAME}.db
restart: unless-stopped
Expand All @@ -49,10 +49,10 @@ services:
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
- ./config/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
MYSQL_USER: davis
MYSQL_PASSWORD: Derived49!
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-Derived49!}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE:-laravel}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
Expand All @@ -78,14 +78,11 @@ services:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data

# Volume definitions
volumes:
dbdata:
driver: local
portainer_data:
driver: local

#Docker Networks
networks:
app-network:
driver: bridge
app-network:
2 changes: 1 addition & 1 deletion init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# /bin/bash

cp .env.example .env
docker-compose up -d --build
docker-compose up -d