0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LaravelをDockerで環境構築!amazon-linuxを使用した場合!

Posted at

amazon-linuxのイメージでDocker環境を構築

Dockerfile

FROM amazonlinux:2023

# 設定
ENV TZ=Asia/Tokyo \
    ROOT=/var/www/lucidia \
    APP_ROOT=/var/www/lucidia/src

WORKDIR $ROOT

# PHP と必要拡張のインストール
RUN dnf update -y \
    && dnf install -y \
    nginx \
    php8.3-fpm \
    php8.3 \
    php8.3-pdo \
    php8.3-mysqlnd \
    php8.3-gd \
    php8.3-bcmath \
    php8.3-zip \
    php8.3-mbstring \
    php8.3-opcache \
    php8.3-intl \
    php8.3-xml \
    php8.3-soap \
    git \
    && dnf clean all

# Composer インストール
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Node.js インストール
RUN curl -sL https://rpm.nodesource.com/setup_20.x | bash - \
    && dnf install -y nodejs \
    && node -v \
    && npm -v

# PHP-FPM 用ディレクトリ作成
RUN mkdir -p /run/php-fpm

# nginx 設定
COPY default.conf /etc/nginx/conf.d/default.conf

# ポート
EXPOSE 80

# Entrypoint
COPY ./shell/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

# CMD はサービス起動(ENTRYPOINT の exec "$@" に渡される)
CMD ["nginx", "-g", "daemon off;"]

default.conf

server {
    listen 80;
    server_name localhost;

    root /var/www/lucidia/src/public;
    index index.php index.html;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

docker-compose.yml

version: "3.9"

services:
  amazon-linux:
    container_name: lucidia-amazon-linux
    build: ./docker/amazon-linux
    volumes:
      - ./:/var/www/lucidia
    ports:
      - ${APP_PORT}:80
    depends_on:
      - db
      - mailhog
      - node
    environment:
      - TZ=${TZ}

  node:
    image: node:18
    container_name: lucidia-node
    working_dir: /var/www/lucidia
    volumes:
      - ./src:/var/www/lucidia
    command: sh -c "npm install && npm run dev"
    ports:
      - ${NODE_PORT}:5173
    environment:
      - TZ=${TZ}

  db:
    image: mysql:5.7
    container_name: lucidia-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      TZ: ${TZ}
    platform: linux/amd64
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - ${MYSQL_PORT}:3306

  mailhog:
    image: mailhog/mailhog
    container_name: lucidia-mailhog
    ports:
      - ${MAILHOG_SMTP_PORT}:1025
      - ${MAILHOG_HTTP_PORT}:8025

entrypoint.sh

#!/bin/sh
set -e

# 初回起動処理
if [ ! -f /var/www/lucidia/src/.env ]; then
    cp /var/www/lucidia/src/.env.example /var/www/lucidia/src/.env
    php artisan key:generate
    php artisan migrate --seed
fi

# 毎回起動処理
php-fpm -D
exec "$@"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?