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 b69b98d..28e4557 100644 --- a/config/Dockerfile +++ b/config/Dockerfile @@ -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/ @@ -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 \ @@ -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"] \ No newline at end of file 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 85% rename from docker-compose.yml rename to docker-compose.local.yml index b0e91f4..1561195 100644 --- a/docker-compose.yml +++ b/docker-compose.local.yml @@ -1,8 +1,7 @@ - version: '3' + services: - #PHP Service app: build: context: . @@ -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 @@ -36,8 +37,7 @@ services: networks: - app-network - #MySQL Service - db: + mariadb: image: mysql:8.0 container_name: ${APP_NAME}.db restart: unless-stopped @@ -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: @@ -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 \ 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