docker machineでApacheを起動する


#環境

- MacBookPro
- Catalina
- VirtualBox
- DockerForMac


#今の状態

作成済みであるバーチャルホスト上のDockerホストのステータスが非ACTIVEとなっています。

```
%docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL   SWARM   DOCKER    ERRORS
default   -        virtualbox   Stopped                 Unknown   
```

#バーチャルボックス上のDockerデーモンを起動させる

```
%docker-machine start default
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
```

#バーチャルボックス上のDockerデーモンの起動確認

```
%docker-machine ls                 
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12   
```

#バーチャルボックス上のDockerデーモンへ接続する

```
%docker-machine env          
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/da-shibata/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env)
```

上記の環境変数を、下記のcommandでまとめて設定することで接続される。

```
%eval $(docker-machine env)
```

#バーチャルボックス上のDockerホストでApacheのコンテナを起動する

```
%docker run --name my-apache-app -p 8080:80 -v $PWD:/usr/local/apache2/htdocs/ httpd:2.4
Unable to find image 'httpd:2.4' locally
2.4: Pulling from library/httpd
45b42c59be33: Already exists 
83ac8490fcc3: Pull complete 
bdb2d204d86d: Pull complete 
243acf75a504: Pull complete 
8fc1ad93a9b1: Pull complete 
Digest: sha256:283f3c833adde108fc3d15c2ccac4b0ad4b650bc28821eafa02eb61f4d6a2c93
Status: Downloaded newer image for httpd:2.4
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
[Fri Mar 12 11:20:37.907850 2021] [mpm_event:notice] [pid 1:tid 140564998808704] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Fri Mar 12 11:20:37.913294 2021] [core:notice] [pid 1:tid 140564998808704] AH00094: Command line: 'httpd -D FOREGROUND'
```

-dを付け忘れるとFOREGROUNDで実行されてしまうので、こうやってプロンプトが返ってこなくなる。

#Apacheの起動を確認する

起動はOK
$PWDを指定したことで、カレントディレクトリが公開されてしまいましたが、実際に公開するときはホスト側のディレクトリを選びましょう。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/391107/86630284-4832-8429-b6b4-da978c988b2c.png)

---

※ここからはdocker machineの操作ではありません。ホストOSでApacheコンテナを起動している例です。

#ログを確認する方法

##Apacheをバックグラウンドで起動する

```
% docker run -d --name apache -p 8080:80 -v $PWD:/usr/local/apache2/htdocs/ httpd:2.4
Unable to find image 'httpd:2.4' locally
2.4: Pulling from library/httpd
6f28985ad184: Pull complete 
3a141a09d1d0: Pull complete 
1633384edb75: Pull complete 
acb3e3b931b8: Pull complete 
f6dc6b8b1d70: Pull complete 
Digest: sha256:9625118824bc2514d4301b387c091fe802dd9e08da7dd9f44d93ee65497e7c1c
Status: Downloaded newer image for httpd:2.4
2563eba7e66ce7eb964049413d55effc7b54020ccf0f04337a71dfff7dfcee5d
```

## docker logs コマンドを実行する

docker logs の後にコンテナIDかコンテナ名を指定して実行する。

###コンテナ名で指定した場合

```
% docker logs apache
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
[Fri Mar 12 17:47:51.738784 2021] [mpm_event:notice] [pid 1:tid 139856445576320] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Fri Mar 12 17:47:51.738995 2021] [core:notice] [pid 1:tid 139856445576320] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [12/Mar/2021:17:48:47 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:48:59 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
% 
```

###コンテナIDで指定した場合

コンテナIDは頭2桁くらい指定すればOK

```
% docker logs 25    
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
[Fri Mar 12 17:47:51.738784 2021] [mpm_event:notice] [pid 1:tid 139856445576320] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Fri Mar 12 17:47:51.738995 2021] [core:notice] [pid 1:tid 139856445576320] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [12/Mar/2021:17:48:47 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:48:59 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:49:17 +0000] "GET / HTTP/1.1" 200 1507
172.17.0.1 - - [12/Mar/2021:17:50:08 +0000] "-" 408 -
% 
```