通过docker机器启动Apache
环境
-
- MacBookPro
-
- Catalina
-
- VirtualBox
- DockerForMac
当前的情况
在已创建的虚拟主机上,Docker主机的状态为非活动。
%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)
通过下面的命令来集中设置上述环境变量以建立连接。
%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是否启动
起動正常。
由于指定了$PWD,当前目录已公开。但实际公开时,请选择主机端的目录。

以下不涉及docker machine的操作。这是在主机操作系统上启动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位即可。
% 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 -
%