Docker for Macにて、httpd:alpineのApacheを使ってみた

初めてDockerをインストールし、Alpine LinuxApacheイメージを使ってみた時のメモです。

今回は以下の記事が参考になりました。ありがとうございました。
Dockerコマンドメモ - Qiita

目次

 

環境

 

Docker for Macのインストール

公式サイトから、stableチャンネルのDocker for Macをダウンロード・インストールします。
Get started with Docker for Mac - Docker

 

Visual Studio CodeでDockerfileを書く準備

Dockerfileのシンタックスハイライトなどを使いたいため、拡張Docker Supportを入れます。
Working with Dockerfiles in Visual Studio Code

 

Alpine LinuxApacheイメージをダウンロード

Docker Hubに行くとDocker Storeが案内されますので、そちらの内容に従って作業します。
httpd - Docker Store

 
今回はAlpine Linux版のApacheイメージを使うため、docker pullします。

# ダウンロード
$ docker pull httpd:2.4.25-alpine

2.4.25-alpine: Pulling from library/httpd
...
Status: Downloaded newer image for httpd:2.4.25-alpine


# 確認
$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               2.4.25-alpine       4fa77c368fa6        2 weeks ago         94.8 MB

 

It Worksを確認

httpd:2.4.25-alpineイメージを使って、Apacheの「It Works」を確認します。

以下のオプションを付けて起動します。

  • pオプション:<ホストのポート>:<dockerのポート>で、ホストとDockerのポートをつなぐ
  • nameオプション:Dockerコンテナに名前をつける
# 起動
$ docker run -p 8081:80 --name it_works httpd:2.4.25-alpine

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[xxxx] [mpm_event:notice] [pid 1:tid 140039857982280] AH00489: Apache/2.4.25 (Unix) configured -- resuming normal operations
[xxxx] [core:notice] [pid 1:tid 140039857982280] AH00094: Command line: 'httpd -D FOREGROUND'


# 確認
$ curl http://localhost:8081

<html><body><h1>It works!</h1></body></html>

 
なお、docker runのオプション位置を間違えるとエラーになり起動しません。

$ docker run -p 8081:80 httpd:2.4.25-alpine --name it_works

container_linux.go:247: starting container process caused "exec: \"--name\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"--name\": executable file not found in $PATH".

   

ローカルのファイルをコピーして起動

次は、DockerfileのCOPYコマンドを使って、Apacheのドキュメントルートへローカルのファイルをコピーして起動してみます。

Docker storeの記載を見ると、Alpine LinuxApacheのドキュメントルートは/usr/local/apache2/htdocs/でした。

./html/hello.html

<!DOCTYPE html>
<html>
<head>
    <meta lang="ja" />
    <meta charset="utf-8" />
    <title>hello</title>
</head>
<body>
    <p>Hello World</p>
</body>
</html>

 
Dockerfile

FROM httpd:2.4.25-alpine

COPY html /usr/local/apache2/htdocs/

 
ホスト側のディレクトリ構成

path/to/root
├── Dockerfile
└── html\
     └── hello.html

 
準備ができたので、Dockerイメージをビルドし、起動します。

# Dockerfileのあるディレクトリにいることを確認
$ ls -l

-rw-r--r--  1 you  staff   68  xxxx Dockerfile
drwxr-xr-x  3 you  staff  102  xxxx html


# Dockerイメージのビルド
$ docker build -t alpine:hello .

Sending build context to Docker daemon  5.12 kB
Step 1/1 : FROM httpd:2.4.25-alpine
 ---> 4fa77c368fa6
Successfully built 4fa77c368fa6


# Dockerイメージができたことを確認
$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              hello               0118b8a13aeb        18 hours ago        94.8 MB
httpd               2.4.25-alpine       4fa77c368fa6        2 weeks ago         94.8 MB


# 起動
docker run -p 8082:80 --name hello_world alpine:hello

 
別のコンソールでドキュメントルートにhello.htmlが存在すること、レスポンスが返ってくることを確認します。

# ドキュメントルートを確認
$ docker exec hello_world ls -al /usr/local/apache2/htdocs

total 16
drwxr-xr-x    1 root     root          4096 xxxx .
drwxr-xr-x    1 www-data www-data      4096 xxxx ..
-rw-r--r--    1 root     root           160 xxxx hello.html
-rw-r--r--    1 501      dialout         45 xxxx index.html


# レスポンスを確認
$ curl http://localhost:8082/hello.html

<!DOCTYPE html>
<html>
<head>
    <meta lang="ja" />
    <meta charset="utf-8" />
    <title>hello</title>
</head>
<body>
    <p>Hello World</p>
</body>
</html>

 

ホストとコンテナでディレクトリを共有

docker runのvオプションを使うことで、ホストとコンテナでディレクトリの共有ができるようです。
DockerのVolume機能について実験してみたことをまとめます - Qiita

そこで、新しくhtmlファイルを作成し、そのディレクトリをコンテナのドキュメントルートで共有してみます。

<!DOCTYPE html>
<html>
<head>
    <meta lang="ja" />
    <meta charset="utf-8" />
    <title>shared</title>
</head>
<body>
    <p>Shared!</p>
</body>
</html>

 
ホストのディレクトリ構成

path/to/root
├── Dockerfile
├── html\
│   └── hello.html
└── htdocs\
    └── host.html

 
vオプションで、<ホストのディレクトリ>:<コンテナのディレクトリ>と指定して起動します。

また、$(pwd)を使って、ホストのカレントディレクトリを取得しています。

$ docker run -p 8083:80 -v $(pwd)/htdocs:/usr/local/apache2/htdocs --name share_file alpine:hello

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[xxxx] [mpm_event:notice] [pid 1:tid 139810871626568] AH00489: Apache/2.4.25 (Unix) configured -- resuming normal operations
[xxxx] [core:notice] [pid 1:tid 139810871626568] AH00094: Command line: 'httpd -D FOREGROUND'

 
別のターミナルで確認します。

# ドキュメントルートの確認
$ docker exec share ls -al /usr/local/apache2/htdocs

total 8
drwxr-xr-x    3 root     root           102 xxxx .
drwxr-xr-x    1 www-data www-data      4096 xxxx ..
-rw-r--r--    1 root     root           161 xxxx host.html


# 動作確認
$ curl http://localhost:8083/host.html

<!DOCTYPE html>
<html>
<head>
    <meta lang="ja" />
    <meta charset="utf-8" />
    <title>shared</title>
</head>
<body>
    <p>Shared!</p>
</body>
</html>

 
試しにホスト側でhost.htmlファイルを修正してみます。

<!--<p>Share file!</p>-->
<p>Update!</p>

 
Apacheを再起動することなく、再度アクセスしてみます。

$ curl http://localhost:8083/host.html

<!DOCTYPE html>
<html>
<head>
    <meta lang="ja" />
    <meta charset="utf-8" />
    <title>shared</title>
</head>
<body>
    <!--<p>Share file!</p>-->
    <p>Update!</p>
</body>
</html>

 
更新が反映されていました。

 

不要なものを削除

色々と試したので、コンテナやイメージを削除します。
Dockerイメージとコンテナの削除方法 - Qiita

# コンテナの全削除
$ docker rm $(docker ps -a -q)

7adf86edc245
70b091a4b7c6


# 削除されたことを確認
$ docker ps -al

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


# イメージの削除
## 削除前の確認
$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              hello               0118b8a13aeb        18 hours ago        94.8 MB
httpd               2.4.25-alpine       4fa77c368fa6        3 weeks ago         94.8 MB
ubuntu              16.04               104bec311bcd        6 weeks ago         129 MB


## 削除
$ docker rmi 0118b8a13aeb

Untagged: alpine:hello
Deleted: sha256:0118b8a13aebcc3572c7ade1fbffbb3b4f23240327b988bd5e9ef88e280b249d
Deleted: sha256:35611424853777d0ec0de35ef64c8fceb90c60d373f64d4775d301605b04d939


## 削除されたか確認
$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               2.4.25-alpine       4fa77c368fa6        3 weeks ago         94.8 MB