diff --git a/.gitignore b/.gitignore index 9d5dff3..3269239 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ Homestead.json /.vagrant .phpunit.result.cache +# src Ordner mit Laravel Intallations Files +src/ diff --git a/README.md b/README.md index e55d924..b9a2b4b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,87 @@ -# Docker_LAPP_Stack +# docker-compose-laravel +A pretty simplified Docker Compose workflow that sets up a LEMP network of containers for local Laravel development. You can view the full article that inspired this repo [here](https://dev.to/aschmelyun/the-beauty-of-docker-for-local-laravel-development-13c0). -Dockerized LAPP Stack \ No newline at end of file +## Usage + +To get started, make sure you have [Docker installed](https://docs.docker.com/docker-for-mac/install/) on your system, and then clone this repository. + +Next, navigate in your terminal to the directory you cloned this, and spin up the containers for the web server by running `docker-compose up -d --build app`. + +After that completes, follow the steps from the [src/README.md](src/README.md) file to get your Laravel project added in (or create a new blank one). + +**Note**: Your MySQL database host name should be `mysql`, **not** `localhost`. The username and database should both be `homestead` with a password of `secret`. + +Bringing up the Docker Compose network with `app` instead of just using `up`, ensures that only our site's containers are brought up at the start, instead of all of the command containers as well. The following are built for our web server, with their exposed ports detailed: + +- **nginx** - `:80` +- **mysql** - `:3306` +- **php** - `:9000` +- **redis** - `:6379` +- **mailhog** - `:8025` + +Three additional containers are included that handle Composer, NPM, and Artisan commands *without* having to have these platforms installed on your local computer. Use the following command examples from your project root, modifying them to fit your particular use case. + +- `docker-compose run --rm composer update` +- `docker-compose run --rm npm run dev` +- `docker-compose run --rm artisan migrate` + +## Permissions Issues + +If you encounter any issues with filesystem permissions while visiting your application or running a container command, try completing one of the sets of steps below. + +**If you are using your server or local environment as the root user:** + +- Bring any container(s) down with `docker-compose down` +- Replace any instance of `php.dockerfile` in the docker-compose.yml file with `php.root.dockerfile` +- Re-build the containers by running `docker-compose build --no-cache` + +**If you are using your server or local environment as a user that is not root:** + +- Bring any container(s) down with `docker-compose down` +- In your terminal, run `export UID=$(id -u)` and then `export GID=$(id -g)` +- If you see any errors about readonly variables from the above step, you can ignore them and continue +- Re-build the containers by running `docker-compose build --no-cache` + +Then, either bring back up your container network or re-run the command you were trying before, and see if that fixes it. + +## Persistent MySQL Storage + +By default, whenever you bring down the Docker network, your MySQL data will be removed after the containers are destroyed. If you would like to have persistent data that remains after bringing containers down and back up, do the following: + +1. Create a `mysql` folder in the project root, alongside the `nginx` and `src` folders. +2. Under the mysql service in your `docker-compose.yml` file, add the following lines: + +``` +volumes: + - ./mysql:/var/lib/mysql +``` + +## Usage in Production + +While I originally created this template for local development, it's robust enough to be used in basic Laravel application deployments. The biggest recommendation would be to ensure that HTTPS is enabled by making additions to the `nginx/default.conf` file and utilizing something like [Let's Encrypt](https://hub.docker.com/r/linuxserver/letsencrypt) to produce an SSL certificate. + +## Compiling Assets + +This configuration should be able to compile assets with both [laravel mix](https://laravel-mix.com/) and [vite](https://vitejs.dev/). In order to get started, you first need to add ` --host 0.0.0.0` after the end of your relevant dev command in `package.json`. So for example, with a Laravel project using Vite, you should see: + +```json +"scripts": { + "dev": "vite --host 0.0.0.0", + "build": "vite build" +}, +``` + +Then, run the following commands to install your dependencies and start the dev server: + +- `docker-compose run --rm npm install` +- `docker-compose run --rm --service-ports npm run dev` + +After that, you should be able to use `@vite` directives to enable hot-module reloading on your local Laravel application. + +Want to build for production? Simply run `docker-compose run --rm npm run build`. + +## MailHog + +The current version of Laravel (9 as of today) uses MailHog as the default application for testing email sending and general SMTP work during local development. Using the provided Docker Hub image, getting an instance set up and ready is simple and straight-forward. The service is included in the `docker-compose.yml` file, and spins up alongside the webserver and database services. + +To see the dashboard and view any emails coming through the system, visit [localhost:8025](http://localhost:8025) after running `docker-compose up -d site`. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..185193a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,137 @@ +version: '3' + +networks: + rauru_net: + +volumes: + local_pgdata_rauru: + pgadmin-data: + +services: + app: + container_name: rauru_app + build: + context: ./dockerfiles + dockerfile: nginx.dockerfile + args: + - UID=${UID:-1000} + - GID=${GID:-1000} + ports: + - "80:80" + volumes: + - ./src:/var/www/html:delegated + depends_on: + - php + - redis + - db + - mailhog + - pgadmin + networks: + - rauru_net + db: + image: postgres + container_name: rauru_db + ports: + - "5432:5432" + environment: + POSTGRES_DB: rauru_db + POSTGRES_USER: rauru_user + POSTGRES_PASSWORD: secret + volumes: + - local_pgdata_rauru:/var/lib/postgresql/data + #- ./rauru_pgdata:/var/lib/postgresql/data + networks: + - rauru_net + pgadmin: + image: dpage/pgadmin4 + container_name: rauru_pgAdmin + ports: + - "5050:80" + depends_on: + - db + environment: + PGADMIN_DEFAULT_EMAIL: admin@example.de + PGADMIN_DEFAULT_PASSWORD: secret + volumes: + - pgadmin-data:/var/lib/pgadmin + #- ./pgadmindata:/var/lib/pgadmin + networks: + - rauru_net + php: + build: + context: ./dockerfiles + dockerfile: php.root.dockerfile + args: + - UID=${UID:-1000} + - GID=${GID:-1000} + container_name: rauru_php + ports: + - "9000:9000" + volumes: + - ./src:/var/www/html:delegated + networks: + - rauru_net + + redis: + image: redis:alpine + container_name: rauru_redis + restart: unless-stopped + ports: + - "6379:6379" + networks: + - rauru_net + + composer: + build: + context: ./dockerfiles + dockerfile: php.root.dockerfile + args: + - UID=${UID:-1000} + - GID=${GID:-1000} + container_name: rauru_composer + volumes: + - ./src:/var/www/html + depends_on: + - php + entrypoint: [ 'composer', '--ignore-platform-reqs' ] + networks: + - rauru_net + + npm: + image: node:current-alpine + container_name: rauru_npm + volumes: + - ./src:/var/www/html + ports: + - "3000:3000" + - "3001:3001" + - "5173:5173" + working_dir: /var/www/html + entrypoint: [ 'npm' ] + networks: + - rauru_net + + artisan: + build: + context: ./dockerfiles + dockerfile: php.root.dockerfile + args: + - UID=${UID:-1000} + - GID=${GID:-1000} + container_name: rauru_artisan + volumes: + - ./src:/var/www/html:delegated + depends_on: + - db + entrypoint: [ 'php', '/var/www/html/artisan' ] + networks: + - rauru_net + + mailhog: + container_name: rauru_mailhog + image: mailhog/mailhog:latest + ports: + - "1025:1025" + - "8025:8025" + networks: + - rauru_net diff --git a/dockerfiles/nginx.dockerfile b/dockerfiles/nginx.dockerfile new file mode 100644 index 0000000..977e2f0 --- /dev/null +++ b/dockerfiles/nginx.dockerfile @@ -0,0 +1,18 @@ +FROM nginx:stable-alpine + +ARG UID +ARG GID + +ENV UID=${UID} +ENV GID=${GID} + +# MacOS staff group's gid is 20, so is the dialout group in alpine linux. We're not using it, let's just remove it. +RUN delgroup dialout + +RUN addgroup -g ${GID} --system laravel +RUN adduser -G laravel --system -D -s /bin/sh -u ${UID} laravel +RUN sed -i "s/user nginx/user laravel/g" /etc/nginx/nginx.conf + +ADD ./nginx/default.conf /etc/nginx/conf.d/ + +RUN mkdir -p /var/www/html \ No newline at end of file diff --git a/dockerfiles/nginx/default.conf b/dockerfiles/nginx/default.conf new file mode 100644 index 0000000..282633d --- /dev/null +++ b/dockerfiles/nginx/default.conf @@ -0,0 +1,20 @@ +server { + listen 80; + index index.php index.html; + server_name _; + root /var/www/html/public; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/dockerfiles/php.dockerfile b/dockerfiles/php.dockerfile new file mode 100644 index 0000000..c834ac6 --- /dev/null +++ b/dockerfiles/php.dockerfile @@ -0,0 +1,38 @@ +FROM php:8-fpm-alpine + +ARG UID +ARG GID + +ENV UID=${UID} +ENV GID=${GID} + +RUN mkdir -p /var/www/html + +WORKDIR /var/www/html + +COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer + +# MacOS staff group's gid is 20, so is the dialout group in alpine linux. We're not using it, let's just remove it. +RUN delgroup dialout + +RUN addgroup -g ${GID} --system laravel +RUN adduser -G laravel --system -D -s /bin/sh -u ${UID} laravel + +RUN sed -i "s/user = www-data/user = laravel/g" /usr/local/etc/php-fpm.d/www.conf +RUN sed -i "s/group = www-data/group = laravel/g" /usr/local/etc/php-fpm.d/www.conf +RUN echo "php_admin_flag[log_errors] = on" >> /usr/local/etc/php-fpm.d/www.conf + +RUN set -ex \ + && apk --no-cache add \ + postgresql-dev + +RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql + +RUN mkdir -p /usr/src/php/ext/redis \ + && curl -L https://github.com/phpredis/phpredis/archive/5.3.4.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \ + && echo 'redis' >> /usr/src/php-available-exts \ + && docker-php-ext-install redis + +USER laravel + +CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"] diff --git a/dockerfiles/php.root.dockerfile b/dockerfiles/php.root.dockerfile new file mode 100644 index 0000000..ab5e324 --- /dev/null +++ b/dockerfiles/php.root.dockerfile @@ -0,0 +1,26 @@ +FROM php:8-fpm-alpine + +RUN mkdir -p /var/www/html + +WORKDIR /var/www/html + +COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer + +RUN sed -i "s/user = www-data/user = root/g" /usr/local/etc/php-fpm.d/www.conf +RUN sed -i "s/group = www-data/group = root/g" /usr/local/etc/php-fpm.d/www.conf +RUN echo "php_admin_flag[log_errors] = on" >> /usr/local/etc/php-fpm.d/www.conf + +RUN set -ex \ + && apk --no-cache add \ + postgresql-dev + +RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql + +RUN mkdir -p /usr/src/php/ext/redis \ + && curl -L https://github.com/phpredis/phpredis/archive/5.3.4.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \ + && echo 'redis' >> /usr/src/php-available-exts \ + && docker-php-ext-install redis + +USER root + +CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]