Skip to content

Instantly share code, notes, and snippets.

@yanker
Last active July 9, 2024 07:22
Show Gist options
  • Save yanker/1418dc1399d44c8381e18a61f3f2bf2f to your computer and use it in GitHub Desktop.
Save yanker/1418dc1399d44c8381e18a61f3f2bf2f to your computer and use it in GitHub Desktop.
Laravel-Docker-July-2024

Install Laravel with Docker

1. Create file docker-compose-yml in the root directory with this dates:

services:
  # Web Server Service
  # Servicio de Nginx
  nginx:
    image: nginx:alpine
    container_name: nginx-laravel11
    ports:
      - '80:80' # Mapea el puerto 80 del contenedor al puerto 8080 del host
    volumes:
      - ./src:/var/www # Mapea el código fuente al contenedor
      - ./nginx/conf.d/:/etc/nginx/conf.d/ # Mapea la configuración de Nginx
    depends_on:
      - php
      - db
    networks:
      - laravel
    environment:
      - NGINX_HOST=laravel.test
      # - NGINX_PORT=80 NO LE GUSTA ESTA VARIABLE DE ENTORNO

  # Database Service
  db:
    # If you're using Mac with ARM architecture processor uncomment the below code
    # platform: linux/x86_64
    image: mysql:5.7
    container_name: mysql
    restart: unless-stopped
    tty: true
    volumes:
      - ./mysql/data:/var/lib/mysql
    ports:
      - '3306:3306'
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      SERVICE_TAGS: ${SERVICE_TAGS}
      SERVICE_NAME: ${SERVICE_NAME}
    networks:
      - laravel

  #phpMyAdmin Service
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: unless-stopped
    ports:
      - '3400:80'
    depends_on:
      - db
    networks:
      - laravel

  # PHP Service
  php:
    build:
      context: ./php
      dockerfile: Dockerfile
    volumes:
      - ./src:/var/www
    ports:
      - '9000:9000'
    networks:
      - laravel

networks:
  laravel:
    driver: bridge

2. Create file .ENV

  MYSQL_DATABASE=db_name
  MYSQL_USER=db_user
  MYSQL_PASSWORD=db_pass
  MYSQL_ROOT_PASSWORD=db_rootpass
  SERVICE_TAGS=dev
  SERVICE_NAME=mysql

3. Create nginx/conf.d/default.conf

server {
  listen 80;
  index index.php index.htm index.html;
  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;
  server_name $NGINX_HOST;
  root /var/www/public;

  location / {
      try_files $uri $uri/ /index.php?$query_string;
  }

  location /index.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;

  }
}

4. Create dockerfile in source /php/dockerfile (without extension file)

FROM php:8.2.11-fpm

RUN docker-php-ext-install pdo pdo_mysql

5. Build DockerFile

docker-compose build

6. Run Docker Compose

docker-compose up -d

7. Create Laravel Project

composer create-project laravel/laravel src

8. Run App

NOTE: add in windows/system32/drivers/etc/host this line -> 127.0.0.1 laravel.test

localhost
o
laravel.test

9. Run phpMyAdmin

http://127.0.0.1:3400

10. Exec Migrations or Others commands

docker-compose exec php bash
cd ..
php artisan migrate
exit
@yanker
Copy link
Author

yanker commented Jul 9, 2024

Add custom url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment