首先可以启动一个 alpine 镜像。
启动 alpine 的命令为:
docker run -it --rm -p 80:80 alpine /bin/ash
为了对 nginx 进行测试,我们对容器外映射了 80 端口。
安装 nginx
alpine 安装应用的命令为 apk add。
所以我们安装 nginx 的命令就为:
apk add nginx
检查 nginx 安装的情况。
运行命令:nginx -t
如能看到下面的输出,则表明 nginx 安装没有问题。
/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/ #
安装 openrc 组件
为了让 nginx 作为服务来运行,我们需要安装 openrc 组件。
使用的命令为:apk add openrc
/ # apk add openrc
(1/4) Installing ifupdown-ng (0.12.1-r4)
(2/4) Installing libcap2 (2.69-r1)
(3/4) Installing openrc (0.52.1-r2)
Executing openrc-0.52.1-r2.post-install
(4/4) Installing nginx-openrc (1.24.0-r16)
Executing busybox-1.36.1-r15.trigger
OK: 11 MiB in 21 packages
/ #
随后,我们运行命令尝试使用 openrc 启动 nginx。
rc-service nginx status
系统会提示有下面的几个问题。
/ # rc-service nginx status
* You are attempting to run an openrc service on a
* system which openrc did not boot.
* You may be inside a chroot or you may have used
* another initialization system to boot this system.
* In this situation, you will get unpredictable results!
* If you really want to do this, issue the following command:
* touch /run/openrc/softlevel
/ #
第一个问题是 openrc 还没有启动,第二个问题是缺少 softlevel
文件。
要启动 openrc,直接运行 openrc 即可。
在运行 openrc 之前,我们可以使用命令 ls -la /run/
查看在当前服务器上运行的服务。
/ # ls -la /run/
total 12
drwxr-xr-x 1 root root 4096 May 14 20:53 .
drwxr-xr-x 1 root root 4096 May 14 20:52 ..
drwxr-xr-x 2 nginx nginx 4096 May 14 20:53 nginx
/ #
从上面我们可以看到,当前我们只有一个 nginx 进程在运行。
当我们运行 openrc 后,再查看系统,我们可以看到有 openrc 的进程启动了。
/ # openrc
* Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'
Service `machine-id' needs non existent service `dev' [ ok ]
/ # ls -la /run/
total 16
drwxr-xr-x 1 root root 4096 May 14 21:01 .
drwxr-xr-x 1 root root 4096 May 14 20:52 ..
drwxr-xr-x 2 nginx nginx 4096 May 14 20:53 nginx
drwxr-xr-x 14 root root 4096 May 14 21:01 openrc
/ #
针对第二个问题,我们可以通过下面的命令来创建这个文件。
touch /run/openrc/softlevel
nginx 的运行状态
当完成上面所有的操作后,可以运行命令 rc-service nginx status
来查看 nginx 的运行状态。
/ # touch /run/openrc/softlevel
/ # rc-service nginx status
* status: stopped
启动 nginx 进程
运行下面的命令可以来启动 nginx 进程。
rc-service nginx start
服务器的输出为:
/ # rc-service nginx start
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/misc/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/rdma/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 108: can't create /sys/fs/cgroup/systemd/tasks: Read-only file system
* Starting nginx ... [ ok ]
/ #
通过上图,可以看到服务器启动成功了。
如果通过机器的 80 端口进行访问,我们能看到提示的 404 错误。
至此,在 alpine 容器中配置 nginx 并且作为服务启动就完成了。
当然需要注意的是,如果你推出这个容器命令行,刚才所有的操作就失效了,因为我们的容器在推出的时候会被删除。
这和我们容器启动的方式有关,如在启动容器的时候不添加 -rm 参数,那我们就不会有什么问题。