如何在 docker 容器中为 CLI 添加一些内容?
How to prepend something to CLI in docker container?
我想在传递给 docker 容器的 CLI 中添加一些内容。
我希望它 运行 像这样:
docker run -it mstools msbuild.exe --version
但是,为了在内部完成这项工作,我需要在 msbuild.exe 的完整路径前添加单声道,如下所示:
mono /Microsoft.Build.Mono.Debug.14.1.0.0-prerelease/lib/msbuild.exe --version
当我在命令中使用下面的 Dockerfile
时,我得到了这个:
$ docker run -it mstools msbuild.exe --version
msbuild.exe: 1: msbuild.exe: [/usr/bin/mono,: not found
如果我跳进容器查看路径:
$ docker run -it --entrypoint=bash mstools
root@eb47008f092e:/# which mono
/usr/bin/mono
我错过了什么??
Docker 文件:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:${PATH}"
ENTRYPOINT ['/usr/bin/mono', " /Microsoft.Build.Mono.Debug.$VERSION/lib/ $@"]
您得到的错误肯定来自于您在 ENTRYPOINT
exec 表单中使用单引号 '
而不是双引号 "
。
此外,我不认为你提到的 "$@"
措辞会起作用(因为 "$@"
需要一些 shell 来评估它,而在 exec 形式中没有/bin/sh -c …
隐含)。但是 ENTRYPOINT
的 exec 形式绝对是正确的选择。
所以我建议你这样写:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:${PATH}"
COPY entrypoint.sh /usr/src/
RUN chmod a+x /usr/src/entrypoint.sh
ENTRYPOINT ["/usr/src/entrypoint.sh"]
与 entrypoint.sh
包含:
#!/bin/bash
exec /usr/bin/mono "/Microsoft.Build.Mono.Debug.$VERSION/lib/" "$@"
(注意:我暂时没有测试这个示例代码,所以如果您发现一些拼写错误,请发表评论)
基于@ErikMD 的回答的最终工作解决方案:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:/Microsoft.Build.Mono.Debug.$VERSION/lib/tools/:${PATH}"
RUN echo '#!/bin/bash' > /usr/src/entrypoint.sh && echo 'exec /usr/bin/mono "$(which "")" "$@"' >> /usr/src/entrypoint.sh && chmod a+x /usr/src/entrypoint.sh
ENTRYPOINT ["/usr/src/entrypoint.sh"]
输出
docker run -it mstools MSBuild.exe -version
Microsoft (R) Build Engine version 14.1.0.0
Copyright (C) Microsoft Corporation. All rights reserved.
14.1.0.0
我想在传递给 docker 容器的 CLI 中添加一些内容。
我希望它 运行 像这样:
docker run -it mstools msbuild.exe --version
但是,为了在内部完成这项工作,我需要在 msbuild.exe 的完整路径前添加单声道,如下所示:
mono /Microsoft.Build.Mono.Debug.14.1.0.0-prerelease/lib/msbuild.exe --version
当我在命令中使用下面的 Dockerfile
时,我得到了这个:
$ docker run -it mstools msbuild.exe --version
msbuild.exe: 1: msbuild.exe: [/usr/bin/mono,: not found
如果我跳进容器查看路径:
$ docker run -it --entrypoint=bash mstools
root@eb47008f092e:/# which mono
/usr/bin/mono
我错过了什么??
Docker 文件:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:${PATH}"
ENTRYPOINT ['/usr/bin/mono', " /Microsoft.Build.Mono.Debug.$VERSION/lib/ $@"]
您得到的错误肯定来自于您在 ENTRYPOINT
exec 表单中使用单引号 '
而不是双引号 "
。
此外,我不认为你提到的 "$@"
措辞会起作用(因为 "$@"
需要一些 shell 来评估它,而在 exec 形式中没有/bin/sh -c …
隐含)。但是 ENTRYPOINT
的 exec 形式绝对是正确的选择。
所以我建议你这样写:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:${PATH}"
COPY entrypoint.sh /usr/src/
RUN chmod a+x /usr/src/entrypoint.sh
ENTRYPOINT ["/usr/src/entrypoint.sh"]
与 entrypoint.sh
包含:
#!/bin/bash
exec /usr/bin/mono "/Microsoft.Build.Mono.Debug.$VERSION/lib/" "$@"
(注意:我暂时没有测试这个示例代码,所以如果您发现一些拼写错误,请发表评论)
基于@ErikMD 的回答的最终工作解决方案:
FROM centeredge/nuget
ARG VERSION="14.1.0.0-prerelease"
RUN nuget install Microsoft.Build.Mono.Debug -Version $VERSION -Source "https://www.myget.org/F/dotnet-buildtools/"
ENV PATH="/Microsoft.Build.Mono.Debug.$VERSION/lib/:/Microsoft.Build.Mono.Debug.$VERSION/lib/tools/:${PATH}"
RUN echo '#!/bin/bash' > /usr/src/entrypoint.sh && echo 'exec /usr/bin/mono "$(which "")" "$@"' >> /usr/src/entrypoint.sh && chmod a+x /usr/src/entrypoint.sh
ENTRYPOINT ["/usr/src/entrypoint.sh"]
输出
docker run -it mstools MSBuild.exe -version
Microsoft (R) Build Engine version 14.1.0.0
Copyright (C) Microsoft Corporation. All rights reserved.
14.1.0.0