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

zf2sa74q  于 2023-11-17  发布在  Nginx
关注(0)|答案(2)|浏览(246)

我试图创建我自己的Nginx图像,使用apline:3.12.0图像,修复了很多错误后,感谢互联网,我设法做到这一点,一切正常,但问题是当我运行以下命令:

  1. rc-service nginx status
  2. * status: stopped

字符串
当我尝试启动服务时,它会给我如下所示:

  1. rc-service nginx start
  2. * WARNING: nginx is already starting


即使服务已经停止,第二个命令的输出也告诉它已经启动了?!
所以我打开了我的docker-machine的localhost来验证服务是开着还是关着,nginx html页面成功出现。
我试着运行rc-service nginx reload,结果如下:

  1. rc-service nginx reload
  2. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
  3. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
  4. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
  5. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
  6. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
  7. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
  8. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
  9. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
  10. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
  11. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
  12. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
  13. /lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
  14. * nginx: cannot `reload' as it has not been started


下面是nginx -t的输出:

  1. nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful


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

  1. less: can't open '/var/log/nginx/error.log': No such file or directory


这是我的文档:

  1. From alpine:latest
  2. COPY nginx.conf ./tmp
  3. COPY index.html ./tmp
  4. COPY run.bash ./tmp
  5. COPY run2.bash ./tmp
  6. RUN apk update && \
  7. apk add nginx && \
  8. adduser -D -g 'www' www && \
  9. mkdir /www && \
  10. chown -R www:www /var/lib/nginx && \
  11. chown -R www:www /www && \
  12. mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig && \
  13. apk add openrc --no-cache && \
  14. sh tmp/run.bash
  15. cmd sh tmp/run2.bash


run.bash:

  1. mv tmp/nginx.conf /etc/nginx/nginx.conf
  2. mv tmp/index.html /www/index.html


run2.bash:

  1. mkdir /run/openrc
  2. touch /run/openrc/softlevel
  3. mkdir -p /run/nginx
  4. nginx
  5. sh


这是我遵循的指南:
https://wiki.alpinelinux.org/wiki/Nginx
我想知道为什么rc-service nginx reload不工作,即使我的nginx服务在我的docker机器上运行完美,以及为什么rc-service nginx status告诉nginx服务停止,即使它不是?
先谢谢你
顺便说一下,当我运行这个命令nginx -s reload时,它没有任何错误。

v9tzhpje

v9tzhpje1#

经过调试和大量的试验和错误,我找到了一个完美的解决方案,至少对我来说,大卫迷宫的答案是非常有帮助的,但我需要能够重新启动容器内的nginx服务。
当我使用以下命令启动容器时:

  1. docker run -it -p 80:80 -p 443:443 alpine:3.12.0

字符串
我们访问容器 shell ,我们需要下载nginx服务器,和openrc能够使用rc-service命令行。

  1. / # apk update
  2. / # apk add nginx openrc

#ANSWER 1

现在我们将使用以下命令测试nginx服务器是否存在错误:

  1. / # nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)
  4. nginx: configuration file /etc/nginx/nginx.conf test failed


正如你可以从我们得到的输出中看到的,它告诉你应该创建一个丢失的文件或目录,所以让我们创建那个目录:

  1. / # ls -la run/
  2. total 8
  3. drwxr-xr-x 2 root root 4096 Dec 16 10:31 .
  4. drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
  5. / # mkdir /run/nginx/


我们给予我们创建的目录一些权限:

  1. / # chown -R nginx:nginx /run/nginx/
  2. / # chmod 775 /run/nginx/
  3. / # ls -la /run/
  4. total 12
  5. drwxr-xr-x 1 root root 4096 Jan 16 08:15 .
  6. drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
  7. drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:15 nginx


现在我们使用nginx:

  1. / # nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful


让我们测试一下nginx服务是否使用rc-service命令启动:

  1. / # rc-service nginx status
  2. * You are attempting to run an openrc service on a
  3. * system which openrc did not boot.
  4. * You may be inside a chroot or you may have used
  5. * another initialization system to boot this system.
  6. * In this situation, you will get unpredictable results!
  7. * If you really want to do this, issue the following command:
  8. * touch /run/openrc/softlevel


所以从上面的输出中我们看到我们有两个问题,openrc没有 Boot ,并且有一个丢失的文件softlevel

  1. / # ls -la /run/
  2. total 12
  3. drwxr-xr-x 1 root root 4096 Jan 16 08:15 .
  4. drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
  5. drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:22 nginx


让我们从使用openrc启动系统开始,只需输入它本身:

  1. / # openrc
  2. * Caching service dependencies ...
  3. Service `hwdrivers' needs non existent service `dev'
  4. / # ls -la /run/
  5. total 16
  6. drwxr-xr-x 1 root root 4096 Jan 16 08:29 .
  7. drwxr-xr-x 1 root root 4096 Jan 16 08:12 ..
  8. drwxrwxr-x 2 nginx nginx 4096 Jan 16 08:22 nginx
  9. drwxr-xr-x 14 root root 4096 Jan 16 08:29 openrc
  10. / # ls -la /run/openrc/
  11. total 64
  12. drwxr-xr-x 14 root root 4096 Jan 16 08:29 .
  13. drwxr-xr-x 1 root root 4096 Jan 16 08:29 ..
  14. drwxr-xr-x 2 root root 4096 Jan 16 08:29 daemons
  15. -rw-r--r-- 1 root root 11 Jan 16 08:29 depconfig
  16. -rw-r--r-- 1 root root 2895 Jan 16 08:29 deptree
  17. drwxr-xr-x 2 root root 4096 Jan 16 08:29 exclusive
  18. drwxr-xr-x 2 root root 4096 Jan 16 08:29 failed
  19. drwxr-xr-x 2 root root 4096 Jan 16 08:29 hotplugged
  20. drwxr-xr-x 2 root root 4096 Jan 16 08:29 inactive
  21. drwxr-xr-x 2 root root 4096 Jan 16 08:29 options
  22. drwxr-xr-x 2 root root 4096 Jan 16 08:29 scheduled
  23. drwxr-xr-x 2 root root 4096 Jan 16 08:29 started
  24. drwxr-xr-x 2 root root 4096 Jan 16 08:29 starting
  25. drwxr-xr-x 2 root root 4096 Jan 16 08:29 stopping
  26. drwxr-xr-x 2 root root 4096 Jan 16 08:29 tmp
  27. drwxr-xr-x 2 root root 4096 Jan 16 08:29 wasinactive


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

  1. / # touch /run/openrc/softlevel


现在我们的rc-service命令工作得很好:

  1. / # rc-service nginx status
  2. * status: stopped


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

  1. / # rc-service nginx start
  2. * Starting nginx ... [ ok ]


检查是否启动:

  1. / # rc-service nginx status
  2. * status: started

#ANSWER 2

或者你可以直接调用这两个命令行:

  1. / # openrc
  2. * Caching service dependencies ...
  3. Service `hwdrivers' needs non existent service `dev' [ ok ]
  4. / # touch /run/openrc/softlevel

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

  1. / # rc-service nginx status
  2. * status: stopped
  3. / # rc-service nginx start
  4. * /run/nginx: creating directory
  5. * /run/nginx: correcting owner
  6. * Starting nginx ... [ ok ]
  7. / # rc-service nginx status
  8. * status: started

希望我说清楚了。

展开查看全部
hgb9j2n6

hgb9j2n62#

Docker容器通常只运行单个进程。您不需要“启动服务”或“重新启动服务”;通常服务器进程本身就是主容器进程,如果您需要重新启动服务,则需要重新启动整个容器。通常,“服务”类型的命令在Docker上下文中缺少一些关键的基础设施,无法工作。
在你的Dockerfile中,你可以让主命令作为前台进程启动Nginx

  1. # No need for openrc
  2. CMD nginx -g "daemon off;"

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

  1. docker build -t my-nginx .
  2. docker stop nginx
  3. docker rm nginx
  4. docker run --name nginx -p 3333:80 -d my-nginx

相关问题