From 88346b5a3ac6275bcebf0df5611b82c482d506fa Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Wed, 13 Jul 2022 19:01:35 -0400 Subject: [PATCH 1/7] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a65365b6e..7eecacdf3 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # 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). -[![GitNFT](https://img.shields.io/badge/%F0%9F%94%AE-Open%20in%20GitNFT-darkviolet?style=flat)](https://gitnft.quine.sh/app/commits/list/repo/docker-compose-laravel) - ## 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. From 1c9105c609f532522b345b0b1110d8e5f3dcb8b7 Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Sun, 13 Nov 2022 18:22:40 -0500 Subject: [PATCH 2/7] Simplifies docker-compose.yml setup, removes separate root docker-compose file, consolidates composer in php image --- README.md | 28 ++------ docker-compose.root.yml | 107 ------------------------------ docker-compose.yml | 24 ++----- dockerfiles/composer.dockerfile | 15 ----- dockerfiles/nginx.root.dockerfile | 7 -- dockerfiles/php.dockerfile | 2 + dockerfiles/php.root.dockerfile | 4 +- 7 files changed, 18 insertions(+), 169 deletions(-) delete mode 100644 docker-compose.root.yml delete mode 100644 dockerfiles/composer.dockerfile delete mode 100644 dockerfiles/nginx.root.dockerfile diff --git a/README.md b/README.md index 7eecacdf3..5e277b3d4 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ A pretty simplified Docker Compose workflow that sets up a LEMP network of conta 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 site`. +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). -Bringing up the Docker Compose network with `site` 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: +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` @@ -30,7 +30,7 @@ If you encounter any issues with filesystem permissions while visiting your appl **If you are using your server or local environment as the root user:** - Bring any container(s) down with `docker-compose down` -- Rename `docker-compose.root.yml` file to `docker-compose.root.yml`, replacing the previous one +- 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:** @@ -54,28 +54,12 @@ volumes: - ./mysql:/var/lib/mysql ``` -## Using BrowserSync with Laravel Mix +## Usage in Production -If you want to enable the hot-reloading that comes with Laravel Mix's BrowserSync option, you'll have to follow a few small steps. First, ensure that you're using the updated `docker-compose.yml` with the `:3000` and `:3001` ports open on the npm service. Then, add the following to the end of your Laravel project's `webpack.mix.js` file: - -```javascript -.browserSync({ - proxy: 'site', - open: false, - port: 3000, -}); -``` - -From your terminal window at the project root, run the following command to start watching for changes with the npm container and its mapped ports: - -```bash -docker-compose run --rm --service-ports npm run watch -``` - -That should keep a small info pane open in your terminal (which you can exit with Ctrl + C). Visiting [localhost:3000](http://localhost:3000) in your browser should then load up your Laravel application with BrowserSync enabled and hot-reloading active. +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. ## MailHog -The current version of Laravel (8 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. +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.root.yml b/docker-compose.root.yml deleted file mode 100644 index aac0bc95e..000000000 --- a/docker-compose.root.yml +++ /dev/null @@ -1,107 +0,0 @@ -version: '3' - -networks: - laravel: - -services: - site: - build: - context: ./dockerfiles - dockerfile: nginx.root.dockerfile - container_name: nginx - ports: - - 80:80 - volumes: - - ./src:/var/www/html:delegated - depends_on: - - php - - redis - - mysql - - mailhog - networks: - - laravel - - mysql: - image: mariadb:10.6 - container_name: mysql - restart: unless-stopped - tty: true - ports: - - 3306:3306 - environment: - MYSQL_DATABASE: homestead - MYSQL_USER: homestead - MYSQL_PASSWORD: secret - MYSQL_ROOT_PASSWORD: secret - SERVICE_TAGS: dev - SERVICE_NAME: mysql - networks: - - laravel - - php: - build: - context: ./dockerfiles - dockerfile: php.root.dockerfile - container_name: php - volumes: - - ./src:/var/www/html:delegated - networks: - - laravel - - redis: - image: redis:alpine - container_name: redis - restart: unless-stopped - ports: - - 6379:6379 - networks: - - laravel - - composer: - image: composer:2 - container_name: composer - volumes: - - ./src:/var/www/html - working_dir: /var/www/html - depends_on: - - php - user: root - entrypoint: ['composer', '--ignore-platform-reqs'] - networks: - - laravel - - npm: - image: node:13.7 - container_name: npm - volumes: - - ./src:/var/www/html - ports: - - 3000:3000 - - 3001:3001 - working_dir: /var/www/html - entrypoint: ['npm'] - networks: - - laravel - - artisan: - build: - context: ./dockerfiles - dockerfile: php.root.dockerfile - container_name: artisan - volumes: - - ./src:/var/www/html:delegated - depends_on: - - mysql - working_dir: /var/www/html - entrypoint: ['php', '/var/www/html/artisan'] - networks: - - laravel - - mailhog: - image: mailhog/mailhog:latest - container_name: mailhog - ports: - - 1025:1025 - - 8025:8025 - networks: - - laravel diff --git a/docker-compose.yml b/docker-compose.yml index bc731d8e6..ddd70dbaa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,15 +3,15 @@ version: '3' networks: laravel: + services: - site: + app: build: context: ./dockerfiles dockerfile: nginx.dockerfile args: - UID=${UID:-1000} - GID=${GID:-1000} - container_name: nginx ports: - 80:80 volumes: @@ -26,7 +26,6 @@ services: mysql: image: mariadb:10.6 - container_name: mysql restart: unless-stopped tty: true ports: @@ -48,7 +47,6 @@ services: args: - UID=${UID:-1000} - GID=${GID:-1000} - container_name: php volumes: - ./src:/var/www/html:delegated networks: @@ -56,7 +54,6 @@ services: redis: image: redis:alpine - container_name: redis restart: unless-stopped ports: - 6379:6379 @@ -66,31 +63,27 @@ services: composer: build: context: ./dockerfiles - dockerfile: composer.dockerfile + dockerfile: php.dockerfile args: - UID=${UID:-1000} - GID=${GID:-1000} - container_name: composer volumes: - ./src:/var/www/html - working_dir: /var/www/html depends_on: - php - user: laravel - entrypoint: ['composer', '--ignore-platform-reqs'] + entrypoint: [ 'composer', '--ignore-platform-reqs' ] networks: - laravel npm: - image: node:13.7 - container_name: npm + image: node:current-alpine volumes: - ./src:/var/www/html ports: - 3000:3000 - 3001:3001 working_dir: /var/www/html - entrypoint: ['npm'] + entrypoint: [ 'npm' ] networks: - laravel @@ -101,19 +94,16 @@ services: args: - UID=${UID:-1000} - GID=${GID:-1000} - container_name: artisan volumes: - ./src:/var/www/html:delegated depends_on: - mysql - working_dir: /var/www/html - entrypoint: ['php', '/var/www/html/artisan'] + entrypoint: [ 'php', '/var/www/html/artisan' ] networks: - laravel mailhog: image: mailhog/mailhog:latest - container_name: mailhog ports: - 1025:1025 - 8025:8025 diff --git a/dockerfiles/composer.dockerfile b/dockerfiles/composer.dockerfile deleted file mode 100644 index 12ec12f37..000000000 --- a/dockerfiles/composer.dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM composer:2 - -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 - -WORKDIR /var/www/html diff --git a/dockerfiles/nginx.root.dockerfile b/dockerfiles/nginx.root.dockerfile deleted file mode 100644 index 1b1cf9100..000000000 --- a/dockerfiles/nginx.root.dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM nginx:stable-alpine - -RUN sed -i "s/user nginx/user root/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/php.dockerfile b/dockerfiles/php.dockerfile index a5f7be283..d71db3d4f 100644 --- a/dockerfiles/php.dockerfile +++ b/dockerfiles/php.dockerfile @@ -10,6 +10,8 @@ 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 diff --git a/dockerfiles/php.root.dockerfile b/dockerfiles/php.root.dockerfile index 05e6625a5..8ab7f1fd3 100644 --- a/dockerfiles/php.root.dockerfile +++ b/dockerfiles/php.root.dockerfile @@ -4,6 +4,8 @@ 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 @@ -15,4 +17,4 @@ RUN mkdir -p /usr/src/php/ext/redis \ && echo 'redis' >> /usr/src/php-available-exts \ && docker-php-ext-install redis -CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"] +CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"] \ No newline at end of file From 42b319ae51b98c8889b5f5c984ea96c3c14630cc Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Sun, 13 Nov 2022 18:47:13 -0500 Subject: [PATCH 3/7] Adds default vite port mapping, adds info about compiling assets with vite or laravel-mix --- README.md | 20 ++++++++++++++++++++ docker-compose.yml | 1 + 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 5e277b3d4..f7d5ede33 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,26 @@ volumes: 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. diff --git a/docker-compose.yml b/docker-compose.yml index ddd70dbaa..67e14440b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -82,6 +82,7 @@ services: ports: - 3000:3000 - 3001:3001 + - 5173:5173 working_dir: /var/www/html entrypoint: [ 'npm' ] networks: From ad0a893cb88d529aae6dca1882e6dc5f6754d42b Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Mon, 14 Nov 2022 00:36:38 -0500 Subject: [PATCH 4/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f7d5ede33..b9a2b4b24 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ Next, navigate in your terminal to the directory you cloned this, and spin up th 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` From 7e08cfbbed30973355b0922a934d6c1baf0f4d64 Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Mon, 14 Nov 2022 01:21:23 -0500 Subject: [PATCH 5/7] Adds quotes around exposed ports, exposes php's 9000 port --- docker-compose.yml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 67e14440b..809ac4a15 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: - UID=${UID:-1000} - GID=${GID:-1000} ports: - - 80:80 + - "80:80" volumes: - ./src:/var/www/html:delegated depends_on: @@ -29,7 +29,7 @@ services: restart: unless-stopped tty: true ports: - - 3306:3306 + - "3306:3306" environment: MYSQL_DATABASE: homestead MYSQL_USER: homestead @@ -47,6 +47,8 @@ services: args: - UID=${UID:-1000} - GID=${GID:-1000} + ports: + - "9000:9000" volumes: - ./src:/var/www/html:delegated networks: @@ -56,7 +58,7 @@ services: image: redis:alpine restart: unless-stopped ports: - - 6379:6379 + - "6379:6379" networks: - laravel @@ -80,9 +82,9 @@ services: volumes: - ./src:/var/www/html ports: - - 3000:3000 - - 3001:3001 - - 5173:5173 + - "3000:3000" + - "3001:3001" + - "5173:5173" working_dir: /var/www/html entrypoint: [ 'npm' ] networks: @@ -106,7 +108,7 @@ services: mailhog: image: mailhog/mailhog:latest ports: - - 1025:1025 - - 8025:8025 + - "1025:1025" + - "8025:8025" networks: - laravel From dbb061879aa73b394a5dfa5620cd88c9bb4f47f4 Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Mon, 14 Nov 2022 01:22:03 -0500 Subject: [PATCH 6/7] Adds laravel user line for potential permissions fixes --- dockerfiles/php.dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dockerfiles/php.dockerfile b/dockerfiles/php.dockerfile index d71db3d4f..2b7d29dfb 100644 --- a/dockerfiles/php.dockerfile +++ b/dockerfiles/php.dockerfile @@ -28,5 +28,7 @@ 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"] From 58f25680e634ff86bf29a9b549f2f05c5a07477d Mon Sep 17 00:00:00 2001 From: Andrew Schmelyun Date: Mon, 14 Nov 2022 01:22:25 -0500 Subject: [PATCH 7/7] Adds user root line to fix potential permissions issues --- dockerfiles/php.root.dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dockerfiles/php.root.dockerfile b/dockerfiles/php.root.dockerfile index 8ab7f1fd3..51205eee0 100644 --- a/dockerfiles/php.root.dockerfile +++ b/dockerfiles/php.root.dockerfile @@ -16,5 +16,7 @@ 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"] \ No newline at end of file +CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]