如何在 alpine:latest image 中使用 rc-service 命令启动 Nginx 服务器

Hol*_*lon 0 service nginx docker dockerfile alpine-linux

我正在尝试Nginx使用apline:latest图像创建自己的图像,在修复了很多错误之后,多亏了互联网,我设法做到了并且一切正常,但是问题是当我运行以下命令时:

rc-service nginx status
 * status: stopped
Run Code Online (Sandbox Code Playgroud)

当我尝试启动服务时,这就是它给我的内容,如下所示:

rc-service nginx start
 * WARNING: nginx is already starting
Run Code Online (Sandbox Code Playgroud)

即使服务停止,第二个命令的输出告诉它已经启动?!

于是我打开了我的docker-machine的localhost来验证服务是开启还是关闭,成功出现nginx html页面。

我尝试运行rc-service nginx reload,结果如下:

rc-service nginx reload
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * nginx: cannot `reload' as it has not been started
Run Code Online (Sandbox Code Playgroud)

这是输出nginx -t

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)

这是less /var/log/nginx/error.log如下所示的输出,没有错误:

less: can't open '/var/log/nginx/error.log': No such file or directory
Run Code Online (Sandbox Code Playgroud)

这是我的 dockerfile :

From alpine:latest

COPY nginx.conf ./tmp
COPY index.html ./tmp
COPY run.bash ./tmp
COPY run2.bash ./tmp

RUN apk update && \
    apk add nginx && \
    adduser -D -g 'www' www && \
    mkdir /www && \
    chown -R www:www /var/lib/nginx && \
    chown -R www:www /www && \
    mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig && \
    apk add openrc --no-cache && \
    sh tmp/run.bash

cmd sh tmp/run2.bash
Run Code Online (Sandbox Code Playgroud)

运行.bash:

mv tmp/nginx.conf /etc/nginx/nginx.conf
mv tmp/index.html  /www/index.html
Run Code Online (Sandbox Code Playgroud)

运行2.bash:

mkdir /run/openrc
touch /run/openrc/softlevel
mkdir -p /run/nginx
nginx
sh
Run Code Online (Sandbox Code Playgroud)

这是我遵循的指南:

https://wiki.alpinelinux.org/wiki/Nginx

我想知道为什么rc-service nginx reload即使我的 nginx 服务在我的 docker 机器上完美运行也不起作用,以及为什么rc-service nginx status即使没有停止 nginx 服务也告诉我nginx 服务已停止?

并提前致谢。

顺便说一句,当我运行此命令时nginx -s reload,它可以正常工作而没有任何错误。

Hol*_*lon 6

经过调试和大量的反复试验,我找到了一个完美的解决方案,至少对我来说,David Maze 的回答非常有帮助,但为了测试目的,我需要我的 shell 可以访问,因为当你运行时nginx -g "daemon off;"你无法访问你的容器 shell除非你停止命令进程。

无论何时我使用以下命令启动我的容器:

docker run -it -p 80:80 -p 443:443 alpine:latest
Run Code Online (Sandbox Code Playgroud)

我们访问容器外壳,我们需要下载nginx服务器,并openrc能够使用rc-service命令行。

/ # apk update
/ # apk add nginx openrc
Run Code Online (Sandbox Code Playgroud)

#答案 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
Run Code Online (Sandbox Code Playgroud)

正如您从我们得到的输出中看到的那样,它告诉您将创建丢失的文件或目录,因此让我们创建该目录:

/ # 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/
Run Code Online (Sandbox Code Playgroud)

我们给那个目录,我们创建了一些权限:

/ # 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
Run Code Online (Sandbox Code Playgroud)

现在我们对 nginx 很好:

/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)

让我们测试我们的 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
Run Code Online (Sandbox Code Playgroud)

所以从上面的输出我们看到我们有两个问题,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
Run Code Online (Sandbox Code Playgroud)

让我们通过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
Run Code Online (Sandbox Code Playgroud)

现在我们创建丢失的文件:

/ # touch /run/openrc/softlevel
Run Code Online (Sandbox Code Playgroud)

现在我们的rc-service命令完美运行:

/ # rc-service nginx status
 * status: stopped
Run Code Online (Sandbox Code Playgroud)

让我们启动我们的服务器:

 / # rc-service nginx start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * Starting nginx ...          [ ok ]
Run Code Online (Sandbox Code Playgroud)

检查它是否已启动:

/ # rc-service nginx status
 * status: started
Run Code Online (Sandbox Code Playgroud)

#答案2

或者您可以简单地调用这两个命令行:

/ # openrc
 * Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'    [ ok ]

/ # touch /run/openrc/softlevel
Run Code Online (Sandbox Code Playgroud)

现在你可以启动你的 nginx 服务器了 :)

/ # rc-service nginx status
 * status: stopped

/ # rc-service nginx start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * /run/nginx: creating directory
 * /run/nginx: correcting owner
 * Starting nginx ...         [ ok ]

/ # rc-service nginx status
 * status: started
Run Code Online (Sandbox Code Playgroud)

希望我很清楚。

  • 这应该是主要 wiki 的一部分,网址为 https://wiki.alpinelinux.org/wiki/Nginx 谢谢! (2认同)

Dav*_*aze 5

Docker 容器通常只运行一个进程。您不需要“启动服务”或“重新启动服务”;一般服务器进程本身就是主容器进程,如果需要重启服务,就重启整个容器。通常,“服务”类型的命令缺少 Docker 上下文中的某些关键基础设施,因此无法工作。

在 Dockerfile 中,您可以将主命令设置为将 Nginx 作为前台进程启动

# No need for openrc
CMD nginx -g "daemon off;"
Run Code Online (Sandbox Code Playgroud)

然后当您需要更改配置时,构建新映像并重新创建容器

docker build -t my-nginx .
docker stop nginx
docker rm nginx
docker run --name nginx -p 3333:80 -d my-nginx
Run Code Online (Sandbox Code Playgroud)