1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

とりあえず、DockerでPHPが起動する環境を作成してみた

1
Last updated at Posted at 2020-01-31

背景

PHP/Laravelでの開発をすることとなり、取り急ぎ学習のためにローカル環境上でPHPが動作する環境を作成してみました。
環境はDockerのコンテナ上で起動するものとして、とりあえずクライアントからコンテナ内のPHPファイルにアクセスして結果を取得できるものとしています。

Laravelプロジェクトの作成については、また後日の予定です。

開発環境

  • Window10
  • Docker Desktop for Windows
  • Git
  • curl

環境構成

  • PHP 7.4
  • MySQL 8
  • Nginx 1.17.8

参考資料

開発環境構築手順

1.docker hubでアカウント作成

2.docker hubからDocker Desktop for Windowsをダウンロード、インストール

3.コマンドでDocker Desktop for Windowsが有効となっていることを確認

>docker -v
Docker version 19.03.5, build 633a0ea

4.GitHubからプロジェクトを任意のリポジトリにクローン

対象のリポジトリは、GitHub - forests-k/training-phpから参照

>git clone https://github.com/forests-k/training-php.git
Cloning into 'training-php'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 17 (delta 0), reused 17 (delta 0), pack-reused 0
Unpacking objects: 100% (17/17), done.

5.Docker Composeコマンドを利用して、コンテナを起動

>cd {project repository}
>docker-compose up -d
Building proxy
Step 1/4 : FROM nginx:1.17.8
 ---> 5ad3bd0e67a9
Step 2/4 : ADD ./server.conf /etc/nginx/conf.d/default.conf
 ---> Using cache
 ---> 28f71cb28a98
Step 3/4 : WORKDIR /var/www
 ---> Using cache
 ---> 5381614cc327
Step 4/4 : RUN apt-get update &&     apt-get install -y vim &&     apt-get install -y curl
 ---> Running in 0df9093a9449

省略

Removing intermediate container 0df9093a9449
 ---> caf465051aeb

Successfully built caf465051aeb
Successfully tagged training-php_proxy:latest
WARNING: Image for service proxy was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building db
Step 1/5 : FROM mysql:8.0
 ---> d0b76af27c77
Step 2/5 : RUN mkdir -p /var/log/mysql
 ---> Running in 9986fc8a5691
Removing intermediate container 9986fc8a5691
 ---> b0b5a95d87c0
Step 3/5 : RUN touch /var/log/mysql/mysqld.log
 ---> Running in 1033fd83c7d1
Removing intermediate container 1033fd83c7d1
 ---> c922d2dc1aa6
Step 4/5 : ADD ./conf.d/my.cnf /etc/mysql/conf.d
 ---> 22ac40181ab9
Step 5/5 : RUN chmod 644 /etc/mysql/conf.d/my.cnf
 ---> Running in 58d7d8522077
Removing intermediate container 58d7d8522077
 ---> a364116c5c58

Successfully built a364116c5c58
Successfully tagged training-php_db:latest
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building php
Step 1/5 : FROM php:7.4-fpm
 ---> 71f166899e8c
Step 2/5 : RUN docker-php-ext-install pdo_mysql
 ---> Using cache
 ---> 2682c04332fe
Step 3/5 : RUN curl -sS https://getcomposer.org/installer | php
 ---> Using cache
 ---> c43a4506b8e5
Step 4/5 : RUN mv composer.phar /usr/local/bin/composer
 ---> Using cache
 ---> 395f230cac8e
Step 5/5 : RUN apt-get update &&     apt-get install -y git
 ---> Using cache
 ---> ee346491d8fb

Successfully built ee346491d8fb
Successfully tagged training-php_php:latest
WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating laravel-training-db    ... done
Creating training-php-fpm       ... done
Creating laravel-training-proxy ... done

6. dokcer-composeコマンドで起動したコンテナを確認する

すべてのコンテナが起動(status = Up)であることを確認する

>docker-compose ps
         Name                       Command              State                 Ports
---------------------------------------------------------------------------------------------------
laravel-training-db      docker-entrypoint.sh mysqld     Up      0.0.0.0:13306->3306/tcp, 33060/tcp
laravel-training-proxy   nginx -g daemon off;            Up      0.0.0.0:81->80/tcp
training-php-fpm         docker-php-entrypoint php-fpm   Up      0.0.0.0:9000->9000/tcp

7.作成したコンテナtraining-php-fpmにログインし、PHPが起動するか確認する

123456789が出力されることを確認する

>docker-compose run php /bin/bash
root@589c4eb21160:/var/www/html# php index.php
123456789

8.クライアントから、リバースプロキシであるNginxを経由してPHPのコンテナにアクセスできるか確認する

レスポンスの値が123456789であることを確認する
※ブラウザから出の確認でも可能

>curl http://127.0.0.1
123456789
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?