From 2d32c9f2ec9dca491b32e730808790e0c8678d19 Mon Sep 17 00:00:00 2001 From: vista Date: Sat, 11 Mar 2023 13:00:48 +0100 Subject: [PATCH] simple laravel container --- .env.example | 19 +++++++++++++++ .gitignore | 5 ++++ build/Dockerfile | 25 ++++++++++++++++++++ build/start-container | 22 ++++++++++++++++++ docker-compose.yml | 54 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 build/Dockerfile create mode 100644 build/start-container create mode 100644 docker-compose.yml diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8b1213a --- /dev/null +++ b/.env.example @@ -0,0 +1,19 @@ +# the laravel version to install (empty for latest) +LARAVEL_VERSION= + +# weather to run "npm dev run" at boot +AUTO_START_NPM_DEV=true + +# external ports +FORWARD_LARAVEL_PORT=80 +FORWARD_VITE_PORT=5173 +FORWARD_DB_PORT=3306 +FORWARD_REDIS_PORT=6379 +FORWARD_MAILPIT_PORT=1025 +FORWARD_MAILPIT_DASHBOARD_PORT=8025 + +# DB settings (this will overwrite the defaults in the laravel project) +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=laravel +DB_PASSWORD=password diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..456b0e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +.env.* +!*.env.example + +.idea diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..5099144 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,25 @@ +FROM composer:latest + +ARG USERNAME=laravel +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +RUN apk add --no-cache nodejs npm zlib-dev libpng-dev \ + && npm install -g npm \ + && rm -rf /tmp/* /var/tmp/* + +RUN docker-php-ext-install mysqli pdo pdo_mysql gd + +RUN addgroup -g $USER_GID -S $USERNAME \ + && adduser -u $USER_UID -S $USERNAME -G $USERNAME + +COPY start-container /usr/local/bin/start-container +RUN chmod +x /usr/local/bin/start-container + +EXPOSE 8080 + +WORKDIR /var/www/html + +USER $USERNAME + +ENTRYPOINT ["start-container"] \ No newline at end of file diff --git a/build/start-container b/build/start-container new file mode 100644 index 0000000..83775ca --- /dev/null +++ b/build/start-container @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# exit when any command fails +set -e + +if [ ! -f /var/www/html/artisan ]; then + echo "No existing Laravel project found" + composer create-project laravel/laravel /var/www/html "$LARAVEL_VERSION" + npm install -D tailwindcss postcss autoprefixer --prefix /var/www/html + sed -i "/export default defineConfig({/a\ server: {host: '0.0.0.0'}," /var/www/html/vite.config.js + echo -e "@tailwind base;\n@tailwind components;\n@tailwind utilities;" >> /var/www/html/resources/css/app.css + echo -e '/** @type {import('tailwindcss').Config} */\nmodule.exports = {\n content: [\n "./resources/**/*.blade.php",\n "./resources/**/*.js",\n "./resources/**/*.vue",\n ],\n theme: {\n extend: {},\n },\n plugins: [],\n}' > /var/www/html/tailwind.config.js + npx tailwindcss init -p +fi + +if [[ "$AUTO_START_NPM_DEV" = true ]]; then + echo "Staring npm dev" + npm run dev --prefix /var/www/html & +fi + +echo "Staring Laravel" +php /var/www/html/artisan serve --host=0.0.0.0 --port=8080 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d610b78 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,54 @@ +version: '3' +services: + app: + build: + context: build + dockerfile: Dockerfile + image: laravel + ports: + - '${FORWARD_LARAVEL_PORT:-80}:8080' + - '${FORWARD_VITE_PORT:-5173}:5173' + environment: + LARAVEL_VERSION: '${LARAVEL_VERSION}' + AUTO_START_NPM_DEV: '${AUTO_START_NPM_DEV:-false}' + DB_HOST: mysql + DB_CONNECTION: mysql + env_file: + .env + depends_on: + - mysql + - redis + - mailpit + + 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' + + redis: + image: 'redis:alpine' + ports: + - '${FORWARD_REDIS_PORT:-6379}:6379' + volumes: + - 'redis:/data' + + mailpit: + image: 'axllent/mailpit:latest' + ports: + - '${FORWARD_MAILPIT_PORT:-1025}:1025' + - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' + +volumes: + mysql: + driver: local + redis: + driver: local