41
45

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 3 years have passed since last update.

Docker Composeを使ってLAMP環境を構築する

Last updated at Posted at 2019-04-30

##Docker Composeを使ってテスト環境を構築する
###【テスト環境の要件】
####初期目標

  • サーバー:apache
  • PHP:7系
  • DB:MySQL

以上の連携

####最終目標
上記に加えて、

  • Composer
  • PHPUnit

を使用可能にする。

##というわけで今回前編
###【Apache/PHPとMySQL、PHPMyAdminのコンテナを立ち上げる】
こちらのエントリ
がまるっとしたいことそのままでした。。。
ただ、この状態だとWebサーバー側でMySQLが使えないので、Webサーバー用コンテナについてはDockerfileを作ることに。

####準備するもの

  • Apache
    • PHP
  • PHPMyAdmin
  • MySQL

#####ファイル構成

├── docker-compose.yml
├── html
├── mysql
├── php7.2
│   ├── dockerfile
│   └── php.ini
└── phpmyadmin

#####docker-compose.yml

docker-compose.yml
version: '3'

services:
  php:
    build:
      context: ./php7.2
      dockerfile: dockerfile
    volumes:
      - ./php7.2/php.ini:/usr/local/etc/php/php.ini
      - ./html:/var/www/html
    ports:
      - 8000:80
    links:
      - mysql:mysql
  mysql:
    image: mysql:5.7
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - 13306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=mysql
      - PMA_USER=test
      - PMA_PASSWORD=test
    links:
      - mysql
    ports:
      - 4000:80
    volumes:
      - ./phpmyadmin/sessions:/sessions

#####php.ini

php7.2/php.ini
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"

#####dockerfile

php7.2/dockerfile
FROM php:7.2-apache
RUN apt-get update && \
  docker-php-ext-install pdo_mysql mysqli mbstring

参考

####1.コンテナ起動

terminal:起動
$ cd _dockertest
$ docker-compose up -d
Creating network "_dockertest_default" with the default driver
Pulling php (php:7.2-apache)...
7.2-apache: Pulling from library/php
27833a3ba0a5: Pulling fs layer
27833a3ba0a5: Pull complete
2d79f6773a3c: Pull complete
/*以下略*/
terminal:動作を確認
$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                            NAMES
a17c50b4dfe2        php:7.2-apache          "docker-php-entrypoi…"   6 seconds ago       Up 5 seconds        0.0.0.0:8000->80/tcp             dockertest_php_1
ad7d69493eff        phpmyadmin/phpmyadmin   "/run.sh supervisord…"   2 minutes ago       Up 2 minutes        9000/tcp, 0.0.0.0:4040->80/tcp   dockertest_phpmyadmin_1
22ecd5cda07a        mysql:5.7               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        3306/tcp, 33060/tcp              dockertest_mysql_1

####2.アクセスしてみる
#####Webサーバー
http://localhost:8000/

スクリーンショット 2019-04-29 18.43.41.png

コンテナに入りたい場合は**$ docker exec -it コンテナ名 bash**で入れる。

terminal
$ docker exec -it dockertest_php_1 bash
root@a17c50b4dfe2:/var/www# ls -l
total 0
drwxr-xr-x 3 root root 96 Apr 29 09:33 html
root@a17c50b4dfe2:/var/www# cd html
root@a17c50b4dfe2:/var/www/html# ls -l
total 4
-rwxr-xr-x 1 root root 17 Apr 11 07:56 index.php
root@a17c50b4dfe2:/var/www/html# exit
exit

#####PHPMyAdmin
http://localhost:4040/index.php?lang=ja

スクリーンショット 2019-04-29 18.43.54.png
41
45
1

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
41
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?