From 15ffea1f4a13a814ca4d6f6119bf775bcd674a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Naglis?= Date: Thu, 15 Feb 2024 11:07:35 +0200 Subject: [PATCH 1/4] Remove redundant image layers for lesser image weight --- config/Dockerfile | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/config/Dockerfile b/config/Dockerfile index b69b98d..ae68e56 100644 --- a/config/Dockerfile +++ b/config/Dockerfile @@ -26,21 +26,17 @@ 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 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 +# 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 contents -COPY ./src /var/www - -# Copy existing application directory permissions +# Copy existing application directory with proper ownership COPY --chown=www:www ./src /var/www # Change current user to www From bf2ab7d5ae5b87992b94e52e76373658b6108f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Naglis?= Date: Thu, 15 Feb 2024 11:20:01 +0200 Subject: [PATCH 2/4] wip configurability for PHP container --- config/Dockerfile | 19 +++++++++++++------ docker-compose.yml | 11 +++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/config/Dockerfile b/config/Dockerfile index ae68e56..d95efad 100644 --- a/config/Dockerfile +++ b/config/Dockerfile @@ -1,5 +1,12 @@ FROM php:8.0-fpm +ARG uid 1000 +ARG guid 1000 + +ENV USER www +ENV GROUP www +ENV PORT 9000 + # Copy composer.lock and composer.json COPY ./src/composer.lock ./src/composer.json /var/www/ @@ -30,18 +37,18 @@ 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 and set proper UID/GUID -RUN groupadd -g 1000 www && useradd -u 1000 -ms /bin/bash -g www www +RUN groupadd -g ${guid} ${USER} && useradd -u ${uid} -ms /bin/bash -g ${GROUP} ${USER} # Copy existing application directory with proper ownership -COPY --chown=www:www ./src /var/www +COPY --chown=${USER}:${GROUP} ./src /var/www # Change current user to www -USER www +USER ${USER} -# Expose port 9000 and start php-fpm server -EXPOSE 9000 +# Expose port and start php-fpm server +EXPOSE ${PORT} CMD ["php-fpm"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b0e91f4..4051bf5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ - version: '3' + services: #PHP Service @@ -17,6 +17,9 @@ services: environment: SERVICE_NAME: app SERVICE_TAGS: dev + USER: ${PHP_USER} + GROUP: ${PHP_GROUP} + PORT: ${PHP_PORT} working_dir: /var/www networks: - app-network @@ -49,9 +52,9 @@ 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_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} + MYSQL_DATABASE: ${MYSQL_DATABASE:-laravel} MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-Derived49!} SERVICE_TAGS: dev SERVICE_NAME: mysql From 9a1b19cf7995cbeb8f718585f922845edc0e93ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Naglis?= Date: Wed, 6 Mar 2024 00:50:22 +0200 Subject: [PATCH 3/4] wip --- .env.example | 15 ++++++++++++++- .gitignore | 5 ++++- README.md | 4 +--- config/Dockerfile | 12 +++++++----- config/mysql/init.sql | 8 -------- docker-compose.yml => docker-compose.local.yml | 12 +++--------- init.sh | 2 +- 7 files changed, 30 insertions(+), 28 deletions(-) delete mode 100644 config/mysql/init.sql rename docker-compose.yml => docker-compose.local.yml (90%) diff --git a/.env.example b/.env.example index d03f4cc..80377c8 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,14 @@ -APP_NAME=Laravel \ No newline at end of file +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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 13d4ac5..baaf260 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .env -.idea/ \ No newline at end of file +.idea/ + +# Project files should be excluded, this is only Docker setup repository +src \ No newline at end of file diff --git a/README.md b/README.md index 1e2ba39..f288f0d 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +- Command delegator script for running commands within appropriate containers \ No newline at end of file diff --git a/config/Dockerfile b/config/Dockerfile index d95efad..82b13f6 100644 --- a/config/Dockerfile +++ b/config/Dockerfile @@ -1,10 +1,12 @@ -FROM php:8.0-fpm +FROM php:8.3-fpm ARG uid 1000 ARG guid 1000 -ENV USER www -ENV GROUP www +ENV UID ${uid} +ENV GUID ${guid} +ENV USER www-data +ENV GROUP www-data ENV PORT 9000 # Copy composer.lock and composer.json @@ -41,13 +43,13 @@ RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl \ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Add user for Laravel application and set proper UID/GUID -RUN groupadd -g ${guid} ${USER} && useradd -u ${uid} -ms /bin/bash -g ${GROUP} ${USER} +RUN groupadd -g 1000 www && useradd -u 1000 -ms /bin/bash -g www www # Copy existing application directory with proper ownership COPY --chown=${USER}:${GROUP} ./src /var/www # Change current user to www -USER ${USER} +USER www # Expose port and start php-fpm server EXPOSE ${PORT} diff --git a/config/mysql/init.sql b/config/mysql/init.sql deleted file mode 100644 index 4a4e7aa..0000000 --- a/config/mysql/init.sql +++ /dev/null @@ -1,8 +0,0 @@ --- create the databases -CREATE DATABASE IF NOT EXISTS laravel; - --- create the users for each database -CREATE USER 'davis'@'%' IDENTIFIED BY 'Derived49!'; -GRANT CREATE, ALTER, INDEX, LOCK TABLES, REFERENCES, UPDATE, DELETE, DROP, SELECT, INSERT ON `laravel`.* TO 'davis'@'%'; - -FLUSH PRIVILEGES; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.local.yml similarity index 90% rename from docker-compose.yml rename to docker-compose.local.yml index 4051bf5..1561195 100644 --- a/docker-compose.yml +++ b/docker-compose.local.yml @@ -2,7 +2,6 @@ version: '3' services: - #PHP Service app: build: context: . @@ -24,7 +23,6 @@ services: networks: - app-network - # Web server nginx: image: nginx:alpine container_name: ${APP_NAME}.nginx @@ -39,8 +37,7 @@ services: networks: - app-network - #MySQL Service - db: + mariadb: image: mysql:8.0 container_name: ${APP_NAME}.db restart: unless-stopped @@ -55,7 +52,7 @@ services: MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE:-laravel} - MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-Derived49!} + MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} SERVICE_TAGS: dev SERVICE_NAME: mysql networks: @@ -81,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 \ No newline at end of file + app-network: \ No newline at end of file diff --git a/init.sh b/init.sh index 50b2285..a58e331 100755 --- a/init.sh +++ b/init.sh @@ -1,4 +1,4 @@ # /bin/bash cp .env.example .env -docker-compose up -d --build \ No newline at end of file +docker-compose up -d \ No newline at end of file From d7b84f798f714b0a73749c9d5a809ae0daf7924c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Naglis?= Date: Sat, 18 May 2024 10:34:38 +0300 Subject: [PATCH 4/4] wip --- config/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/Dockerfile b/config/Dockerfile index 82b13f6..28e4557 100644 --- a/config/Dockerfile +++ b/config/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.3-fpm +FROM php:8.3-fpm-alpine ARG uid 1000 ARG guid 1000 @@ -16,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 \