Dockerfile版本控制最佳实践 [英] Dockerfile versioning best practice

查看:71
本文介绍了Dockerfile版本控制最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是目前正在开发C ++应用程序的一些开发人员。

We are a few developers currently developing a C++ app.

为了确保每个人都使用与远程生产服务器相同的库和依赖项,我们

In order to be sure that everyone use the same libraries and dependencies than the remote production server, we are using docker to compile the code source in our localhost.

我的问题是在docker中使用git的最佳实践是什么?

My question is what the best practices to use git with docker?


  1. 将Dockerfile添加到源代码存储库中

  2. 为我们所有的Dockerfile创建专用存储库

  3. 已创建每个Dockerfile专用的存储库

  4. 其他?

  1. Add the Dockerfile to the source code repository
  2. Create a dedicated repository for all our Dockerfiles
  3. Created a dedicated repository for each Dockerfile
  4. Others?


推荐答案

将您的Dockerfile与源代码保持在一起。我们使用标签将版本信息添加到生成的图像。我们添加:

Keep your Dockerfile with the source code. We use labels to add versioning info to the produced image. We add:


  • git commit and branch

  • 是否为脏,这意味着更改是由git中的内容在src代码上本地制作的

  • CI版本号(公开可见)

  • 创建图像的人(而不是人)

  • the git commit and branch
  • whether it's "dirty" meaning that changes were made locally on the src code from what's in git
  • a CI version number (publicly visible)
  • the person who built the image (not the person who last checked in git)

我们还用提交编号标记了图像。

We also tag the image with the commit number.

这是我们其中一项服务的代码。我们正在为我们的CI使用 Buildkite Quay.io 用于我们的图像注册表。

Here's our code for one of our services. We're using Buildkite for our CI and Quay.io for our image registry.

build-image.sh

echo '===> Building docker image...'

GIT_BRANCH=$(git name-rev --name-only HEAD | sed "s/~.*//")
GIT_COMMIT=$(git rev-parse HEAD)
GIT_COMMIT_SHORT=$(echo $GIT_COMMIT | head -c 8)
GIT_DIRTY='false'
BUILD_CREATOR=$(git config user.email)
BUILD_NUMBER="${BUILDKITE_BUILD_NUMBER-0}"
# Whether the repo has uncommitted changes
if [[ $(git status -s) ]]; then
    GIT_DIRTY='true'
fi

docker build \
  -q \
  -t quay.io/myco/servicename:latest \
  -t quay.io/myco/servicename:"$GIT_COMMIT_SHORT" \
  --build-arg GIT_BRANCH="$GIT_BRANCH" \
  --build-arg GIT_COMMIT="$GIT_COMMIT" \
  --build-arg GIT_DIRTY="$GIT_DIRTY" \
  --build-arg BUILD_CREATOR="$BUILD_CREATOR" \
  --build-arg BUILD_NUMBER="$BUILD_NUMBER" \
  .

echo "Done"
echo "Push to quay using:"
echo "  docker push quay.io/myco/servicename:latest"
echo "  docker push quay.io/myco/servicename:$GIT_COMMIT_SHORT"

Dockerfile

FROM ...

ARG GIT_COMMIT
ARG GIT_BRANCH=master
ARG GIT_DIRTY=undefined
ARG BUILD_CREATOR
ARG BUILD_NUMBER

LABEL branch=$GIT_BRANCH \
    commit=$GIT_COMMIT \
    dirty=$GIT_DIRTY \
    build-creator=$BUILD_CREATOR \
    build-number=$BUILD_NUMBER

... etc

然后,您可以制作检查图像版本的脚本。例如:

Then you can make scripts that check the version of your image. Eg:

docker inspect --format "{{.ContainerConfig.Labels.commit}}" imageid

这篇关于Dockerfile版本控制最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆