docker 运行 with ENTRYPOINT 包含一个变量

docker run with ENTRYPOINT containing a variable

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

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中称为"context"):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 在我的示例中起作用的任何想法?

ENTRYPOINT 不进行 ENV 替换,但是 WORKDIR does:

WORKDIR ${INSTALL_DIR}
ENTRYPOINT ./hello_world

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