[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

dockerコンテナで構成するLAMP環境 ~初級編~

こんにちは。
開発チームのワイルド担当、まんだいです。

dockerコンテナは本来、一つのdockerコンテナで完結して稼働するものではなく
複数のdockerコンテナが協調して動いて一つのサービスを動かすものです。

まずは、1サーバー内にある2つのdockerコンテナが連携して動くウェブシステムを作ってみたいと思います。

なお、こちらの記事はdockerの基礎をさらってdockerを理解した気になるdocker入門シリーズ、その4となります。
過去のdocker入門シリーズをお読みになりたい場合は、以下のリンクより辿ってみてください。

 

今回のゴール

今回は、dockerを使って原始的なLAMP構成を組む事をゴールに進めていきます。
原始的なLAMP構成がどういうものか、僕の頭にある構成を説明すると

  • apache+PHPが稼働しているdockerコンテナが一つ
  • MySQLが稼働しているdockerコンテナが一つ
  • 別々のコンテナを繋いであたかもローカル上にインストールされたように機能し
  • VM外から見ると普通のウェブシステムがそれっぽく動いている

こんな状況を想像しています。

これによって

  • 複数のコンテナを立ち上げる方法(って程のものではないですが)が分かります
  • コンテナ同士を接続する方法が分かります
  • これってコンテナを別々のサーバーに置いてもいいんじゃないかと、ふと思い浮かびます

みたいな事がわかればいいなと思います。

適当にwordpressでもインストールしてみて動けばいいなと思っています。
今回も前回同様、virtualboxのubuntu上で作業を進めていきます。

 

必要なdockerイメージを用意する

今回必要なdockerイメージは

  • MySQLのdockerイメージ
  • apache + PHPのdockerイメージ

の2つです。
前者は、MySQLの公式dockerイメージ、後者はwordpress入りの公式dockerイメージを使用します。

それでは、MySQLのdockerイメージをdocker hubからpullし、dockerコンテナを起動します。

vagrant@vagrant:~$ docker run --name mymysql -e MYSQL_ROOT_PASSWORD=[mysqlのパスワード] -d mysql:latest
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql

86e6c3163927: Pull complete
68f4b3625ea4: Pull complete
04f7e78a2c8a: Pull complete
1bade56c3b6b: Pull complete
dd6387e14c18: Pull complete
ca30c0626c9b: Pull complete
0dc5e226a795: Pull complete
6c164b0f04cb: Pull complete
5c74d058f7b5: Pull complete
0fd3b6e12567: Pull complete
e8126a9d061e: Pull complete
d17cffff8039: Pull complete
1924f4186d05: Pull complete
14961e5db73a: Pull complete
Digest: sha256:16de02081c408c41361126aaa718f91693688d39a216a74ac8dab841db050228
Status: Downloaded newer image for mysql:latest
12cb7271b424e8043d7bb36484061f49331a87be25bcd415e4a9a481a6b33c53

dockerコンテナの起動を確認してみます。

vagrant@vagrant:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED         STATUS          PORTS                  NAMES
12cb7271b424        mysql:latest        "/entrypoint.sh mysql"   3 seconds ago   Up 3 seconds    3306/tcp               mymysql

無事起動していますね。
続いてwordpress入りのdockerイメージをpullし、dockerコンテナを起動します。

vagrant@vagrant:~$ docker run --name mywordpress --link mymysql:mysql -p 8080:80 -d wordpress
Unable to find image 'wordpress:latest' locally
latest: Pulling from library/wordpress

9482852a6953: Pull complete
81de90fd7f09: Pull complete
57ae0639bf23: Pull complete
c4f7968ac19e: Pull complete
d06608e0df69: Pull complete
7ab219b6a3ea: Pull complete
049be7b00a71: Pull complete
1ac57811ebb0: Pull complete
5f7f35d35a61: Pull complete
e7ff06ab86e9: Pull complete
50cbe3cba4bd: Pull complete
ef9f3544f906: Pull complete
0544870563ee: Pull complete
6b7e8b5d0ef6: Pull complete
4fb33229649f: Pull complete
d0cabc0feb31: Pull complete
6fb545ed9179: Pull complete
9e9f602b9253: Pull complete
8d3522c82327: Pull complete
26c9007116c8: Pull complete
19faf318b397: Pull complete
04c297e0e874: Pull complete
0b9e66c139d3: Pull complete
52cddd72d1ca: Pull complete
19d7bfb3a2a7: Pull complete
15031c83aa88: Pull complete
9c3238040649: Pull complete
492d7fea8944: Pull complete
676505bb515e: Pull complete
eea959e50c85: Pull complete
1029747e7634: Pull complete
155de5677313: Pull complete
Digest: sha256:bfd7e102741d73cce4ec58b2d937586c670f31df1c80aeaf4d5c525eb3c6ac06
Status: Downloaded newer image for wordpress:latest
8606df07f39f2a22dbdefd916b42c1747ca1df86f4a012620862c039d110887f

終わったら、dockerコンテナが起動しているか確認してみます。

vagrant@vagrant:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8606df07f39f        wordpress           "/entrypoint.sh apach"   3 seconds ago       Up 3 seconds        0.0.0.0:8080->80/tcp   mywordpress
12cb7271b424        mysql:latest        "/entrypoint.sh mysql"   2 minutes ago       Up 2 minutes        3306/tcp               mymysql

以上で必要なもののインストールは完了しました。
wordpressのdockerイメージには、MySQLは含まれていませんが、docker hubの説明を読む限り

  • 「--link mymysql:mysql」のオプションを追加してインストールすると、mymysqlという名前のdockerコンテナを使用するMySQLのコンテナとしてセットアップされるようです。
  • WORDPRESS_DB_HOSTのデフォルト値はリンクしたmysqlコンテナを利用するようになっているので未指定です。
  • WORDPRESS_DB_USERは、ひとまずrootユーザーでmymysqlコンテナにアクセスするので未指定です(変更推奨)。
  • WORDPRESS_DB_PASSWORDは指定しなかった場合、接続したmysqlコンテナの起動時に設定したMYSQL_ROOT_PASSWORD変数をmysqlコンテナから取得するので改めて指定する必要はないです(変更推奨)。
  • WORDPRESS_DB_NAMEはデフォルト値がwordpressになっています。他のDB名がいい場合はその旨記載します。

この辺りを抑えれば、問題なくコンテナを越えてDB接続ができるようです。

 

ブラウザからwordpressのセットアップを行う

改めてブラウザからwordpressのインストール画面を見てみます。
VMのUbuntu上でdockerを稼働させているので、Vagrantfile内に以下を記述し、ホストPCの18080番ポートをVMの8080番ポートにポートフォワードしています。

config.vm.network "forwarded_port", guest: 8080, host: 18080

VMの8080番ポートは、wordpressのdockerコンテナの80番ポートにポートフォワードしているので、18080番からdockerコンテナの80番に繋がるということになります。

http://localhost:18080/ にアクセスすると、セットアップ画面がぬるっと表示されました。

wordpress_installation_01

言語に日本語を選択し、続けるを押下。

wordpress_installation_02
ログインに必要な情報を入力すると

wordpress_installation_03

完了しました。あら簡単。

wordpress_installation_04

続いて、ログインフォームが現れたので、先ほど入力したユーザー名とパスワードを入力すると

wordpress_installation_05

ダッシュボードにたどり着きました。

あっという間に終わってしまいましたが、railsなどでちゃんと作ったウェブサービスなら、git clone、rakeコマンドなどもDockerfileに含んでdockerイメージを作ってしまうと、同じようなパターンでデプロイができてしまうのはすごいですね。

また、DBにAWSのRDSを利用するのもIPアドレスを指定するだけで変更できますし、nginxのdockerコンテナを配置してみるなどのアレンジも非常に簡単です。
全てはdockerコンテナをどうリンクさせるか次第です。
以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
1,402
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.