【问题标题】:docker run with ENTRYPOINT containing a variabledocker 使用包含变量的 ENTRYPOINT 运行
【发布时间】:2015-05-05 10:07:36
【问题描述】:

我在 github.com 上有一个私有 git 存储库,我想使用 Dockerfile 自动化 docker build 进程。我天真地认为拥有Dockerfile 的最佳位置是在我的仓库的根文件夹中。示例:

git clone ssh://git@github.com/avilella/bioboxes_playground
cd bioboxes_playground
ls Dockerfile
Dockerfile

所以repo文件夹中的文件是:

[hpenvy15 bioboxes_playground] $ tree -Difts
.
[      18027 Mar  3 14:58]  ./LICENSE
[         22 Mar  3 14:58]  ./README.md
[        825 Mar  4 11:59]  ./Dockerfile
[       4096 Mar  4 11:59]  ./src
[         74 Mar  4 11:59]  ./src/hello_world.c

所以对于外部依赖项,我使用apt-get install 安装它们或下载tar.gz 并在docker build 期间安装它们。

我从上一个问题中了解到如何将本地文件添加到镜像中(在 Dockerfile 中称为“上下文”):http://kimh.github.io/blog/en/docker/gotchas-in-writing-dockerfile-en/#add_and_understanding_context_in_dockerfile

我在 repo 中添加了一个简单的src/hello_world.c 示例,我想在docker build 期间进行编译。见下文:

已编辑:现在添加 WORKDIR

FROM debian:wheezy
MAINTAINER Foo Bar, foo@bar.com

ENV ORG foo
ENV APP bar
ENV INSTALL_DIR /opt/${ORG}/${APP}

ENV PACKAGES wget binutils make csh g++ sed gawk perl zlib1g-dev 
RUN apt-get update -y && apt-get install -y --no-install-recommends ${PACKAGES}

ENV SEQTK https://github.com/avilella/seqtk/archive/sgdp.tar.gz

ENV THIRDPARTY_DIR ${INSTALL_DIR}/thirdparty

RUN mkdir -p ${THIRDPARTY_DIR}
RUN cd ${THIRDPARTY_DIR}

# SEQTK

RUN mkdir -p ${THIRDPARTY_DIR}/seqtk && cd ${THIRDPARTY_DIR}/seqtk &&\
    wget --quiet --no-check-certificate ${SEQTK} --output-document - |\
    tar xzf - --directory . --strip-components=1 && \
    make

# COMPILE HELLO_WORLD

ADD src/hello_world.c ${INSTALL_DIR}/hello_world.c

RUN gcc ${INSTALL_DIR}/hello_world.c -o ${INSTALL_DIR}/hello_world

WORKDIR ${INSTALL_DIR}
ENTRYPOINT ["./hello_world"]

我目前的问题是我定义的ENTRYPOINT 不起作用 因为INSTALL_DIR 是一个 ENV 变量。我可以将其硬编码为 /opt/foo/bar/hello_world,然后就可以了:

$ sudo docker run -i foo                                                                                                                                  
exec: "./hello_world": stat ./hello_world: no such file or directory2015/03/04 15:43:37 Error response from daemon: Cannot start container cd39493be9f40a8714cbc1e503a1f172e2e5dc485881d0b5e34322fbb2f71380: exec: "./hello_world": stat ./hello_world: no such file or directory

这行得通:

$ sudo docker run --entrypoint='/opt/foo/bar/hello_world' -i foo                                                                                          
Hello World

任何想法如何使 ENTRYPOINT 在我的示例中工作?

【问题讨论】:

    标签: docker dockerfile


    【解决方案1】:

    ENTRYPOINT不做ENV替换,但是WORKDIR does:

    WORKDIR ${INSTALL_DIR}
    ENTRYPOINT ./hello_world
    

    但我想知道您为什么要将 INSTALL_DIR 作为变量?如果您不使用容器,您可以这样做,以便 /opt/foo/bar/opt/foo/baz/opt/qux/bar 可以共存。但如果这些都在一个单独的容器中,那么它们都可以住在/opt/app

    【讨论】:

    • 也许他喜欢这样一个事实,即他可以通过 orgname 和 appname 定义安装目录。除此之外,必须更改这些变量将意味着整个 dockerfile 将重建(所有缓存都无效)。似乎不是很有用。
    • 我明白了。我现在将它更新为您示例中的最后两行,但它仍然对我不起作用。硬编码确实有效。
    • 您使用的是 JSON 格式,但我使用非 JSON 格式编写了示例,因为我依赖 shell 来使用相对路径。你可以说ENTRYPOINT ["/bin/bash","./hello_world"],我想也一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2017-05-13
    • 2021-05-15
    • 2020-01-09
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多