LoginSignup
6
1

More than 1 year has passed since last update.

Docker で LAMP 環境を作ろう 第 1 回

Last updated at Posted at 2021-05-28

こんにちは。
私の開発環境は Windows10 64bit + XAMPP5.6.24 + Homestead (Vagrant + VirtualBox) なんですが、同じチームの他メンバーは Mac な人も多く、XAMPP -> MAMP になり、環境が若干違います。
いまのところ、この環境差異が原因で開発が困難、ということもないんですが、どうせなら同じ環境にしたいじゃないですか。

で、Docker なら Mac と Windows で同じ環境が作れるのでは? と思い、XAMPP5.6.24 環境を Docker 上で再現することにしました。
WSL2 になって Linux 系のコンテナも安定動作する、らしい。
安定動作することが確認出来たら、VirtualBox の Homestead も全部 Docker にすれば PC の負荷が軽くなりそう。
なんてことを考えながら、さっそく Docker Desktop を導入していきます。

まずは Docker Desktop for Windows をインストールする

システム要件
* Windows 10 64 ビット:Pro、Enterprise、Education(ビルド 15063 以上)
* Hyper-V と Windows コンテナ機能の有効化が必要
* 64 ビット SLAT (Second Level Address Translation 対応プロセッサ)
* メモリ 4GB
* BIOS レベルでのハードウェア仮想化
となっていますが、いまどき開発に使っている PC なら多分大丈夫(OS は要注意かも)。

インストール
手順はいたってカンタン、
https://docs.docker.jp/get-started/index.html
を見ながら進めていけば多分問題なくインストールできると思います。
2021-05-26 現在の最新版は version 3.3.3 (2021-05-06) でした

  1. まずは https://hub.docker.com/editions/community/docker-ce-desktop-windows/ の「Get Docker」をクリックして Docker Desktop Installer.exe をダウンロード。
  2. ダウンロードしたファイルをダブルクリックして実行
    Docker00.png

  3. そのまま「Ok」でインストールスタート
    Docker01.png

  4. あとは何も聞かれません。じっと待っていると 1 分ほどで完了
    Docker02.png

テスト用コンテナを動かしてみる

こんな簡単にインストール完了するのか? という感じで、逆に不安になります。確認してみましょう。
適当なシェル(コマンドプロンプトとか Windows PowerShell とか)を開き、まずは version 。

$ docker --version
Docker version 20.10.6, build 370c289

おお。
無事インストールできているようなので、次はお約束の「Hello World」。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

無事 Hello from Docker! と表示されました。
This message shows that your installation appears to be working correctly
というメッセージがわかりにくいけど「正しく動作している」と受け取りましょう。

取得済みのイメージやコンテナの確認

インストール直後は hello-world だけかな。私の作業 PC ではいろいろ出てきます。

$ docker image ls
REPOSITORY                           TAG       IMAGE ID       CREATED         SIZE
candidate-laravel-docker_app         latest    e5065d716c43   2 months ago    500MB
candidate-laravel-docker_candidate   latest    e5065d716c43   2 months ago    500MB
laravel-docker_candidate             latest    e5065d716c43   2 months ago    500MB
php                                  7.3-fpm   806330a9d03b   2 months ago    399MB
php                                  8.0-fpm   b1a7e2e969ca   2 months ago    407MB
mysql                                5.7       f8fcde8b9ae2   2 months ago    449MB
mysql                                latest    14340cbfa999   2 months ago    546MB
bulletinboard                        1.0       d99683843ba0   2 months ago    178MB
adminer                              latest    54d5528d5e4b   2 months ago    89.8MB
hello-world                          latest    d1165f221234   2 months ago    13.3kB
ubuntu                               latest    4dd97cefde62   2 months ago    72.9MB
theqwan/php                          7.3-stg   3970edc6a9f5   2 months ago    680MB
alpine/git                           latest    a939554ad0d0   3 months ago    25.1MB
nginx                                latest    35c43ace9216   3 months ago    133MB
php                                  7.2-fpm   28f52b60203d   5 months ago    398MB
shincoder/homestead                  php7.3    f2ef80f2d33a   20 months ago   892MB
nginx                                1.15.6    e81eb098537d   2 years ago     109MB
$ docker ps --all
CONTAINER ID   IMAGE                                COMMAND                  CREATED         STATUS                     PORTS                               NAMES
cd75fa57d03c   hello-world                          "/hello"                 7 minutes ago   Exited (0) 7 minutes ago                                       nice_pike
783576418ca4   ubuntu:latest                        "/bin/bash"              7 weeks ago     Exited (255) 5 weeks ago                                       gracious_jones
2a86a363c2cc   nginx:1.15.6                         "nginx -g 'daemon of…"   2 months ago    Exited (255) 5 weeks ago   0.0.0.0:80->80/tcp                  candidate-laravel-docker_api_1
6c897ec8c196   candidate-laravel-docker_candidate   "docker-php-entrypoi…"   2 months ago    Exited (255) 5 weeks ago   9000/tcp                            candidate-laravel-docker_candidate_1
db867bb8b00f   mysql:5.7                            "docker-entrypoint.s…"   2 months ago    Exited (255) 5 weeks ago   0.0.0.0:3306->3306/tcp, 33060/tcp   candidate-laravel-docker_lvnmatch-mysql_1
2e46ffebe015   3970edc6a9f5                         "/init"                  2 months ago    Exited (0) 2 months ago                                        homestead

次回は実際に動作する Web アプリのコンテナを取得して動作させます。

参考 URL

残念なことにこの記事よりよほど参考になります。

Docker で LAMP 環境を作ろう 第 1 回
Docker で LAMP 環境を作ろう 第 2 回
Docker で LAMP 環境を作ろう 第 3 回

6
1
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
6
1