0
0

More than 1 year has passed since last update.

はじめての記事投稿

PHP初心者がつまづきながらもDocker + CakePHPの環境構築をしてみた

Posted at

どうも、こんにちは。mokomokoです。
インフラを2年間やってから開発現場へ移って3ヶ月がたった新米エンジニアです。
業務ではCodeigniterを触っているのですが、なにか他のPHPフレームワークも触ってみたいと思ったため、Docker + CakePHPで簡単なアプリとDBだけの環境を作ってみます。

やること

・DockerでCakePHPの環境構築

ディレクトリ構成

 .
├── /docker
│  └── Dockerfile
├── /html
└── docker-compose.yml

ラクしたい方は下記コードをそのままシェルにコピペして実行してください

mkdir docker   
touch docker/Dockerfile 
mkdir html 
touch docker-compose.yml

docker-compose.yml

version: "3.7"
services:
  cakeapp:
    build:
      context: ./docker
      dockerfile: Dockerfile
    ports:
      - "8080:80"
    tty: true
    stdin_open: true
    volumes:
      - ./html:/var/www/html

  db:
    image: mysql:5.7
    volumes:
      - "./db:/docker-entrypoint-initdb.d"
    environment:
      MYSQL_DATABASE: database
      MYSQL_USER: mysql000user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password

Dockerfile

FROM php:7.3-apache

RUN apt-get update \
    && apt-get install -y unzip libicu-dev \
    && docker-php-ext-install intl \
    && docker-php-ext-install pdo_mysql \
    && a2enmod rewrite \
    && apt-get clean \
    && rm -fr /var/lib/apt/lists/*

とりあえず構築してみる

docker compose up -d
docker ps   # コンテナID確認する 
docker exec -it {CONTAINER ID} bash # コンテナ内へ入る

# pharファイルを置いてプロジェクト起動
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php composer.phar create-project --prefer-dist cakephp/app:4.* html

http://localhost/8080

Forbidden
You don't have permission to access this resource.

と、エラーがでた。
コンテナのログを調べると,,,

docker logs {CONTAINE ID} # ログ出力

> Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive

なんかアクセスしてる場所が違うっぽい。
ディレクトリを見ると最初に作ったhtml配下にCakePHPで作成されたhtmlが入っていたので
http://localhost/8080/html
でオープンできた。
(www/配下でcomposerすればよかっただけ、、)

DBと接続

app_local.php

'Datasources' => [
        'default' => [
            'host' => 'db', # docker-composeのservice名
            ~~~ 省略 ~~~
            //'port' => 'non_standard_port_number',

            'username' => 'mysql000user',
            'password' => 'password',

            'database' => 'database',
            /*
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /*
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],

これで環境構築おしまい。

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