docker 在包含 Dockerfile 的私有 git 存储库上构建?

docker build on a private git repo containing the Dockerfile?

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

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

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

我不清楚的是如何将 repo 中的代码暴露给 docker build 中的说明。

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

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

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

# define an entry point...

我认为可行的选项:

  1. 在执行 docker build 时,我以某种方式将 repo 文件夹安装在 docker 中,以便它看到我刚刚从我的 repo 克隆的代码,在本例中为 hello_world.c 文件,然后它将遵循 Dockerfile 指令。我该怎么做?

  2. 我认为的另一个选择是做一个新的 git clone 作为 Dockerfile 指令的一部分。

  3. 第三种选择是发布我的 git 存储库,并使用 wget 下载该版本的副本作为 Dockerfile 说明。

以下哪种方法最好?有什么更好的做法的想法吗?

实际上您是对的 - 在存储库的根目录中包含 Docker 文件确实是最佳做法。工作流程如下所示:

  1. 像您在问题中所做的那样,克隆存储库并切换到目录。
  2. 运行 docker build -t <some tag> . 构建 docker 图像
  3. 现在使用 docker push <some tag> 将 docker 图像推送到您的图像存储库(无论是在 Docker Hub public/private 还是其他地方)

请注意,如果您希望自动构建新图像,则需要转向其他服务。 Docker Hub 有一个 automated builds 功能,它将监视您的 git 存储库并在指定分支上检测到新提交时构建映像。

在 Docker 文件中,您应该使用 ADD 指令将您的代码复制到容器中。

希望对您有所帮助。

1:我同意 Ben Whaley ADD 是正确的选择。然后,您可以在存储库的根文件夹中使用 docker build .

2 和 3:每次在 hello_world.c 上进行更改时,您都必须提交并推送到您的远程(如果这是一个私人仓库,您还必须添加 ssh 密钥)。对你来说可能没问题,但在本地修复错误时非常不切实际。

我和你有同样的想法,我花了一些时间才意识到 docker 还不能很好地处理这个问题。

您可以看看我的 shell 脚本 (https://github.com/Equanox/docker-auto-build)。它使您能够轻松地在 git 存储库中构建和 运行 您的 docker 文件。您甚至可以将常用的 运行 cmd 存储在 docker 文件中。您会在此处找到如何使用它的示例。