├── .gitignore ├── LICENSE ├── README.md ├── docker-compose.yml ├── docker ├── nginx.Dockerfile ├── nginx │ └── default.conf ├── php.Dockerfile └── php │ └── www.conf ├── mysql └── empty └── src └── empty /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .env.backup 3 | docker-compose.override.yml 4 | /.idea 5 | /.vscode 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Vitalii S. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Environment Setup for the Latest Version of Laravel (PHP, Nginx, Laravel, MySql) Using Docker 2 | 3 | ### Project Structure 4 | 5 | - `docker` - Folder for all configuration files for docker and other services 6 | - `nginx` - Folder for nginx configuration files 7 | - `php` - Folder for php configuration files 8 | - `src` - Folder where the project code will be stored 9 | - `docker-compose.yml` - Docker compose configuration file 10 | 11 | ### Step-by-Step Guide 12 | 13 | #### 1. Environment Setup 14 | 15 | - Remove empty files from src and mysql dirs. 16 | 17 | ``` 18 | rm src/empty 19 | rm mysql/empty 20 | ``` 21 | 22 | - In docker-compose.yml, change the data to access the database 23 | 24 | ``` 25 | MYSQL_DATABASE: laraveldb 26 | MYSQL_USER: laravel 27 | MYSQL_PASSWORD: secret 28 | MYSQL_ROOT_PASSWORD: secret 29 | ``` 30 | 31 | #### 2. Build the Project Using Docker Compose 32 | 33 | - Run this command 34 | 35 | ``` 36 | docker compose build 37 | ``` 38 | 39 | #### 3. Create a Laravel Project 40 | 41 | - Run this command: 42 | 43 | ``` 44 | docker compose run --rm composer create-project laravel/laravel . 45 | ``` 46 | 47 | - After running this command, the project code should appear in the src folder. 48 | 49 | - Start docker containers 50 | 51 | ``` 52 | docker compose up -d 53 | ``` 54 | 55 | - You can verify if the project is working by opening the browser. For example, if it’s set to 80: 56 | 57 | ``` 58 | http://localhost 59 | ``` 60 | 61 | #### 4. Configure Laravel project 62 | 63 | - Configure Mysql in /src/.env . Uncomment and change: 64 | 65 | ``` 66 | DB_CONNECTION=mysql # connection name, we use mysql 67 | DB_HOST=mysql # name of mysql service in docker-compose.yml 68 | DB_PORT=3306 # mysql standart port 69 | DB_DATABASE=laraveldb # database name from MYSQL_DATABASE in docker-compose.yml 70 | DB_USERNAME=laravel # username from MYSQL_USER in docker-compose.yml 71 | DB_PASSWORD=secret # user password from MYSQL_PASSWORD in docker-compose.yml 72 | ``` 73 | - Restart all services 74 | 75 | ``` 76 | docker compose down 77 | docker compose up -d 78 | ``` 79 | 80 | #### 5. Run Migrations 81 | 82 | ``` 83 | docker compose run --rm artisan migrate 84 | ``` 85 | 86 | #### Some useful commands 87 | 88 | - Enter the php container (php is the name of the service from docker-compose.yml) 89 | 90 | ``` 91 | docker compose run --rm php /bin/sh 92 | ``` 93 | 94 | - If access Forbidden 95 | 96 | ``` 97 | docker compose run --rm php /bin/sh 98 | chown -R laravel:laravel /var/www/html 99 | ``` -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | 3 | nginx: 4 | build: 5 | context: . 6 | dockerfile: ./docker/nginx.Dockerfile 7 | depends_on: 8 | - php 9 | - mysql 10 | container_name: laravel_nginx 11 | ports: 12 | - 80:80 13 | - 443:443 14 | volumes: 15 | - ./src:/var/www/html 16 | 17 | php: 18 | build: 19 | context: . 20 | dockerfile: ./docker/php.Dockerfile 21 | container_name: laravel_php 22 | volumes: 23 | - ./src:/var/www/html 24 | 25 | mysql: 26 | image: mysql:8.0.27 27 | platform: linux/amd64 28 | container_name: laravel_mysql 29 | ports: 30 | - 3306:3306 31 | volumes: 32 | - ./mysql:/var/lib/mysql 33 | environment: 34 | MYSQL_DATABASE: laraveldb 35 | MYSQL_USER: laravel 36 | MYSQL_PASSWORD: secret 37 | MYSQL_ROOT_PASSWORD: secret 38 | 39 | composer: 40 | build: 41 | context: . 42 | dockerfile: ./docker/php.Dockerfile 43 | container_name: laravel_composer 44 | volumes: 45 | - ./src:/var/www/html 46 | working_dir: /var/www/html 47 | entrypoint: ['composer'] 48 | 49 | artisan: 50 | build: 51 | context: . 52 | dockerfile: ./docker/php.Dockerfile 53 | container_name: laravel_artisan 54 | volumes: 55 | - ./src:/var/www/html 56 | working_dir: /var/www/html 57 | entrypoint: ['php', 'artisan'] 58 | 59 | npm: 60 | image: node:current-alpine 61 | container_name: laravel_npm 62 | volumes: 63 | - ./src:/var/www/html 64 | working_dir: /var/www/html 65 | entrypoint: ['npm'] 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /docker/nginx.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:stable-alpine 2 | 3 | COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf 4 | 5 | RUN mkdir -p /var/www/html 6 | -------------------------------------------------------------------------------- /docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | index index.php index.html; 4 | server_name localhost; 5 | root /var/www/html/public; 6 | 7 | location / { 8 | try_files $uri $uri/ /index.php?$query_string; 9 | } 10 | 11 | location ~ \.php$ { 12 | try_files $uri = 404; 13 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 14 | fastcgi_pass php:9000; 15 | fastcgi_index index.php; 16 | include fastcgi_params; 17 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 18 | fastcgi_param PATH_INFO $fastcgi_path_info; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /docker/php.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.2-fpm-alpine 2 | 3 | COPY ./docker/php/www.conf /usr/local/etc/php-fpm.d/www.conf 4 | 5 | RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel 6 | 7 | # Install dependencies and extensions 8 | RUN apk add --no-cache \ 9 | freetype \ 10 | libjpeg-turbo \ 11 | libpng \ 12 | libzip \ 13 | freetype-dev \ 14 | libjpeg-turbo-dev \ 15 | libpng-dev \ 16 | libzip-dev \ 17 | && docker-php-ext-configure gd \ 18 | --with-freetype \ 19 | --with-jpeg \ 20 | && docker-php-ext-install -j$(nproc) \ 21 | gd \ 22 | pdo \ 23 | pdo_mysql \ 24 | zip \ 25 | bcmath \ 26 | && apk del --no-cache \ 27 | freetype-dev \ 28 | libjpeg-turbo-dev \ 29 | libpng-dev \ 30 | libzip-dev 31 | 32 | # Install Composer 33 | COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer 34 | 35 | RUN mkdir -p /var/www/html 36 | 37 | COPY ./src/ /var/www/html 38 | 39 | RUN chown -R laravel:laravel /var/www/html 40 | -------------------------------------------------------------------------------- /docker/php/www.conf: -------------------------------------------------------------------------------- 1 | [www] 2 | 3 | user = laravel 4 | group = laravel 5 | 6 | listen = 127.0.0.1:9000 7 | 8 | pm = dynamic 9 | 10 | pm.max_children = 5 11 | pm.start_servers = 2 12 | pm.min_spare_servers = 1 13 | pm.max_spare_servers = 3 14 | 15 | -------------------------------------------------------------------------------- /mysql/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vshloda/docker-laravel/2481d3c24af26784ad8182889eec87e715191c24/mysql/empty -------------------------------------------------------------------------------- /src/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vshloda/docker-laravel/2481d3c24af26784ad8182889eec87e715191c24/src/empty --------------------------------------------------------------------------------