iT邦幫忙

2023 iThome 鐵人賽

DAY 27
0
自我挑戰組

我推的Laravel系列 第 27

【Day-26】我推的Laravel-實戰篇-Docker

  • 分享至 

  • xImage
  •  

簡介

Docker可以想像將環境容器化
先前有提到,Laravel有提供Sail,但筆者不太喜歡用
原因是:

  1. Sail還是必須安裝PHP、Composer等
  2. Windows不方便使用

所以今天不用Sail,單純用Docker建立起本地環境

安裝

使用Docker前必須安裝Docker,筆者用Windows使用了Docker Desktop
安裝還需要用WSL、Hyper等,請自行安裝

開始

開啟Docker Desktop
https://ithelp.ithome.com.tw/upload/images/20231011/20163286pbUGQBA2Py.png

使用Laravel搭配Docker

nginx\conf.d\app.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

mysql\my.cnf

[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log

docker-compose.yml(專案根目錄下)

version: '3'
services:
  
  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:stable-alpine3.17
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:8
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge
#Volumes
volumes:
  dbdata:
    driver: local

Dockerfile(專案根目錄下)

FROM php:8.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libzip-dev

# Install extensions
RUN docker-php-ext-install pdo_mysql

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

RUN

docker-compose up -d

啟動後,可以進入http://localhost/
https://ithelp.ithome.com.tw/upload/images/20231011/20163286utqIaqrBFh.png

如果要跑php artisan指令

docker-compose exec app php artisan ...

其他一些常用的docker指令
查詢containner

    docker ps -a
    docker logs <app_container_id_or_name>

進入containner

    docker exec -it <container_name_or_id> /bin/bash

删除所有容器(Containers)

    docker rm -f $(docker ps -aq)

删除所有鏡像(Images)

    docker rmi -f $(docker images -aq)

總結

本篇讓讀者能快速起步使用docker架起laravel
當然還有許多地方可以延伸,mysql、redis,當然還有上次Horizon的ext可以嘗試裝好,架起來運作
太多細部的部分,這邊就不多做教學,讀者朝著自己所需去做搭配即可


上一篇
【Day-25】我推的Laravel-實戰篇-Git/Github flow
下一篇
【Day-27】我推的Laravel-實戰篇-部屬到Render
系列文
我推的Laravel31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言