【问题标题】:Openssl command not working in Dockerfile RunOpenssl 命令在 Dockerfile 运行中不起作用
【发布时间】:2020-05-18 23:37:46
【问题描述】:

我是 Docker 新手。我正在尝试通过一些基本操作来增加我的理解。我创建了一个非常基本的 web 应用程序,并希望为它创建一个 Dockerfile。我想做的一件事是通过 https 交付 webapp。我想使用 Let's Encrypt。当我刚刚开始时,我在 Let's Encrypt 的网站上找到了 instructions,用于为本地开发创建证书。

我想把它作为我的 Dockerfile 的一部分。

我将以下内容添加到我的 Dockerfile:

RUN openssl req \
-x509 \
-out localhost.crt \
-keyout localhost.key \
-newkey rsa:2048 \
-nodes \
-sha256 \
-subj "/CN=localhost" \
-extensions EXT \
-config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

不过,每当我在此文件上运行 docker build 命令时,我都会收到以下错误。

Step 12/18 : RUN openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj "/CN=localhost" -extensions EXT -config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
 ---> Running in 079b3085beba
/bin/sh: 1: Syntax error: "(" unexpected
The command '/bin/sh -c openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj "/CN=localhost" -extensions EXT -config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")' returned a non-zero code: 2

由于我的简单应用程序是一个 dotnet 应用程序,所以基础镜像是 FROM mcr.microsoft.com/dotnet/core/aspnet:3.1。这是一个基于 3.1.4-buster-slim 构建的镜像,它是一个 debian 容器。

然后我运行docker run -it mcr.microsoft.com/dotnet/core/aspnet:3.1,以便我可以对该根容器进行shell访问,然后我能够将上述命令复制并粘贴到终端中,它工作得很好。

我在我的 Windows 机器上运行 Docker for Windows,所以我想可能是由于 Windows 和 Linux 之间的行尾不同而导致错误。我确保 Dockerfile 上的所有行结尾都是 LF 而不是 CRLF 并且仍然遇到相同的错误。 Dockerfile 是UTF-8 编码,据我所知,这也是 Linux 的标准。

我不知道为什么会出现此错误。我需要对命令或 dockerfile 进行哪些更改才能使上述命令作为我的 Dockerfile 构建的一部分正常工作?

【问题讨论】:

  • 我怀疑来自buster-slim/bin/sh 与Bash 不兼容。您可能需要根据已安装的 shell 调整命令语法(或安装 Bash/使用带有 Bash 的映像以使用该语法运行命令)
  • 它可能是一个dash shell,请参阅unix.stackexchange.com/questions/310500/… 了解可能的替代语法
  • @michaeldel 感谢您提供这些链接。如果您说的是真的,那么当我使用-it 运行容器时,为什么相同的命令会起作用?那是在容器上运行 bash 命令吗?
  • 另外,作为 linux 新手,这些链接并不能真正帮助我理解我需要做出哪些改变。

标签: docker dockerfile


【解决方案1】:

感谢@michaeldel,我能够理解发生了什么问题。

搜索了一下,发现在一个Dockerfile中,可以指定要使用的shell。我将 Dockerfile 更新为以下内容,现在一切正常。

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
SHELL ["/bin/bash", "-c"]
RUN echo "$(openssl version)"
RUN openssl req \
-x509 \
-out localhost.crt \
-keyout localhost.key \
-newkey rsa:2048 \
-nodes \
-sha256 \
-subj "/CN=localhost" \
-extensions EXT \
-config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多