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

2024-01-06

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

rc-service nginx status
 * status: stopped

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

rc-service nginx start
 * WARNING: nginx is already starting

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

所以我打开了我的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

这是输出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

这是输出less /var/log/nginx/error.log如下图没有错误:

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

这是我的 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

运行.bash:

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

运行2.bash:

mkdir /run/openrc
touch /run/openrc/softlevel
mkdir -p /run/nginx
nginx
sh

这是我遵循的指南:

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

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

并提前致谢。

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


经过调试和大量的试验和错误,我找到了一个至少对我来说完美的解决方案,大卫·梅兹答案非常有帮助,但我需要能够在容器内重新启动 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

希望我说清楚了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 rc-service 命令在 alpine 镜像中启动 Nginx 服务器 的相关文章

随机推荐

  • 日期转换程序中的奇怪错误

    我无法修复代码中的奇怪错误 我编写了一个程序来打印最初以 MM DD YYYY 格式编写的日期 格式为 月 DD YYYY 该程序很简单 我将日期存储在char数组 然后用strtok我分别获取月 日 年并存储指向它们的指针 在一个char
  • 如何将 std::string 传递给需要 char* 的函数? [复制]

    这个问题在这里已经有答案了 可能的重复 我可以从 C 字符串中获取非常量 C 字符串吗 https stackoverflow com questions 1919626 can i get a non const c string bac
  • BASH 中多列文件的反向排序顺序

    我有以下文件 1 2 3 1 4 5 1 6 7 2 3 5 5 2 1 我希望文件按第二列排序 但从最大数字 在本例中为 6 到最小数字 我尝试过 sort 1 2 file dat 但它按升序排序 而不是降序 结果应该是 1 6 7 1
  • 命令是否从 shell 脚本中的当前目录运行?

    在 bash shell 脚本中我尝试了这两个版本 java jar abc jar and CMD java jar abc jar CMD 第一个版本可以工作 第二个版本则抱怨找不到 abc jar 为什么 命令确实从 shell 脚本
  • 使用命令行开关将 PDF 另存为文本 - 可以做到吗?

    我需要使用命令行开关来执行 另存为文本 命令 理想情况下 我想要 使用命令行开关打开 PDF 使用命令行开关通过模仿 另存为文本 命令将 PDF 转换为文本文件 使用命令行关闭 PDF 这可能吗 如果是这样 那么有人知道该怎么做吗 也许你可
  • JavaFX 中的 MVVM。具有数据模型的控件

    我在fxml文件中定义了一个TableView 但我无法指定表的列 因为表将具有一些数据模型知识 这会违反MVVM 我创建了一个类 它提供 TableColumn 的集合并提供数据模型 我想更改相对于表中所选项目的按钮状态 如何在不违反MV
  • 为什么 (0 < 5 < 3) 返回 true?

    我在 jsfiddle net 上玩 我很好奇为什么这会返回 true if 0 lt 5 lt 3 alert True 这也是如此 if 0 lt 5 lt 2 alert True 但这并没有 if 0 lt 5 lt 1 alert
  • 将用户控件转换为服务器控件

    我想知道是否有人有将用户控件转换为 Web 控件的经验 理想情况下 我想将一些设计工作交给其他人 他们会给我精心布置的用户控件 然后 我就可以完成转换 编译 测试和部署的过程 在微软提出神奇的 转换为服务器控制 选项之前 看起来我一直坚持从
  • ImageIcon 的 getResources() - java

    我的第一个问题 几天来我一直试图解决这个问题 但我已经失去了耐心 以下是一些代码和我的项目结构 问题 我怎样才能得到getResources 在 eclipse 中工作并导入到 jar 后 谢谢您的帮助 public enum Icons
  • UIView 子类 initWithFrame 没有被调用?

    我有一个自定义 UIView 子类 在 IB 中 我指定了一个 占位符 UIView 并将该类设置为我的类名 我对 drawRect 方法的重写正在工作 并且背景着色正确 但 initWithFrame 没有触发 为什么 id initWi
  • 是否可以在 iOS 模拟器中进行边缘滑动?

    iOS 7 提供了UIScreenEdgePanGestureRecognizer它检测从屏幕边缘向内的滑动 可以使用Xcode中的iOS7模拟器来模拟这个手势吗 在屏幕区域外单击并拖动只会移动整个模拟器框架 在模拟器版本 11 4 上 我
  • 带有按列搜索字段的 Jhipster 表

    我在我的应用程序中使用 jhipster 我有一个表 我添加了分页和排序 但我还想在列中添加搜索字段 这可能吗 会是这样的http ng table com http ng table com 我的 HTML 是 div class tab
  • 使用 Python 的 Paramiko 自动执行 ssh 连接和程序执行

    我想使用 python 自动执行特定任务 除其他事项外 此任务包括使用 ssh 连接到远程服务器 并运行特定程序 称为prog out that 可能会也可能不会要求用户输入 经过一些研究并权衡我的选择后 我决定使用 Python 的 Pa
  • Google Datastore 强一致性和实体组最大大小

    在共享费用应用程序中 显示每个组的付款费用和共享费用详细信息 作为金融应用 很多操作都是事务性的 这就需要强一致性来保证数据的完整性 我们使用了实体组和祖先查询 这似乎解决了强一致性的问题 这导致了实体组的规模很大 由于共享 团体 现在是成
  • 反应:赋值左侧无效(NULL)

    我正在尝试提取名为tests stats 的个人函数的输出 我做了一个函数return陈述 return c list test 1 list test 2 list test 3 list test 4 其中每个list test i i
  • 如何在地图div之外添加Leaflet Routing Machine控件

    Leaflet 路由机 http www liedman net leaflet routing machine 容器div默认显示在地图上 我想将此div放在地图下方 有什么线索可以做到这一点吗 因此 您需要在地图div之外添加一个传单控
  • css, html help : 左右浮动,切断背景以扩展过去的div

    我有一个向左浮动的 div 和一个向右浮动的 div 我想更改背景颜色 它改变了背景 但它停在浮动 div 之前 当我把它们取下来时 它仍然具有我想要的正确背景颜色 div style border top 333 solid thin b
  • 为什么 Ruby 使用 nil 来命名 null 对象?

    谷歌搜索但没有找到答案 nil 这个名字有什么特别的吗 nil的概念和其他语言中的null有什么不同吗 好吧 nil 是 Lisp 和 Smalltalk 中 无 具体化概念的传统名称 null 一词用作形容词 意思是 空 如 空列表 即
  • 使用 vscode 运行测试时出错:项目文件存在某种问题

    此问题发生在使用 VSCode 调试单个测试 而不是整个套件 的 MacOS 上 当我尝试使用 vscode 调试此测试时 我得到以下输出 usr local share dotnet sdk 6 0 201 Microsoft Commo
  • 如何使用 rc-service 命令在 alpine 镜像中启动 Nginx 服务器

    我正在尝试创建自己的Nginx图像 使用apline 3 12 0图像 在修复了很多错误之后 感谢互联网 我设法做到了 一切正常 但问题是当我运行以下命令时 rc service nginx status status stopped 当我