经过调试和大量的试验和错误,我找到了一个至少对我来说完美的解决方案,大卫·梅兹答案非常有帮助,但我需要能够在容器内重新启动 nginx 服务。
无论如何,当我使用以下命令启动容器时:
docker run -it -p 80:80 -p 443:443 alpine:3.12.0
我们访问容器外壳,我们需要下载nginx
服务器 , 和openrc
能够使用rc-service
命令行。
/ # apk update
/ # apk add nginx openrc
#答案1
现在我们将使用以下命令测试 nginx 服务器是否有错误:
/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
正如您从我们得到的输出中看到的,它告诉您将创建一个丢失的文件或目录,所以让我们创建该目录:
/ # ls -la run/
total 8
drwxr-xr-x 2 root root 4096 Dec 16 10:31 .
drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
/ # mkdir /run/nginx/
我们给我们创建的目录一些权限:
/ # chown -R nginx:nginx /run/nginx/
/ # chmod 775 /run/nginx/
/ # ls -la /run/
total 12
drwxr-xr-x 1 root root 4096 Jan 16 08:15 .
drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:15 nginx
现在我们已经可以使用 nginx 了:
/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
让我们测试一下我们的 nginx 服务是否已启动rc-service
命令 :
/ # 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
:
/ # ls -la /run/
total 12
drwxr-xr-x 1 root root 4096 Jan 16 08:15 .
drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:22 nginx
让我们首先启动我们的系统openrc
只需输入它本身:
/ # openrc
* Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'
/ # ls -la /run/
total 16
drwxr-xr-x 1 root root 4096 Jan 16 08:29 .
drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:22 nginx
drwxr-xr-x 14 root root 4096 Jan 16 08:29 openrc
/ # ls -la /run/openrc/
total 64
drwxr-xr-x 14 root root 4096 Jan 16 08:29 .
drwxr-xr-x 1 root root 4096 Jan 16 08:29 ..
drwxr-xr-x 2 root root 4096 Jan 16 08:29 daemons
-rw-r--r-- 1 root root 11 Jan 16 08:29 depconfig
-rw-r--r-- 1 root root 2895 Jan 16 08:29 deptree
drwxr-xr-x 2 root root 4096 Jan 16 08:29 exclusive
drwxr-xr-x 2 root root 4096 Jan 16 08:29 failed
drwxr-xr-x 2 root root 4096 Jan 16 08:29 hotplugged
drwxr-xr-x 2 root root 4096 Jan 16 08:29 inactive
drwxr-xr-x 2 root root 4096 Jan 16 08:29 options
drwxr-xr-x 2 root root 4096 Jan 16 08:29 scheduled
drwxr-xr-x 2 root root 4096 Jan 16 08:29 started
drwxr-xr-x 2 root root 4096 Jan 16 08:29 starting
drwxr-xr-x 2 root root 4096 Jan 16 08:29 stopping
drwxr-xr-x 2 root root 4096 Jan 16 08:29 tmp
drwxr-xr-x 2 root root 4096 Jan 16 08:29 wasinactive
现在我们创建丢失的文件:
/ # touch /run/openrc/softlevel
now our rc-service
命令完美运行:
/ # rc-service nginx status
* status: stopped
让我们启动我们的服务器:
/ # rc-service nginx start
* Starting nginx ... [ ok ]
检查它是否已启动:
/ # rc-service nginx status
* status: started
#答案2
或者您可以简单地调用这两个命令行:
/ # openrc
* Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev' [ ok ]
/ # touch /run/openrc/softlevel
现在你可以启动你的 nginx 服务器了:)
/ # rc-service nginx status
* status: stopped
/ # rc-service nginx start
* /run/nginx: creating directory
* /run/nginx: correcting owner
* Starting nginx ... [ ok ]
/ # rc-service nginx status
* status: started
希望我说清楚了。