Is Dockerfile versioning a best practice?

0 votes

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.

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

  • Adding the Dockerfile to the source code repository
  • Creating a dedicated repository for all our Dockerfiles
  • Creating a dedicated repository for each Dockerfile
  • Any other way

Can Anyone help me with this?

Jun 17, 2018 in Docker by Atul
• 10,170 points
30 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

According to me , it will be best for you to keep your Dockerfile with the source code. We use labels to add versioning info to the produced image. We add:

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

We also tag the image with the commit number.

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}}" image
I hope the above information will be helpful for you.
answered Jun 17, 2018 by shubham
• 6,480 points

Related Questions In Docker

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to continue running a docker container which is already exited?

Restart an existing container after you exited ...READ MORE

answered Jul 19, 2018 in Docker by Sophie may
• 9,050 points
47 views
0 votes
1 answer

Is it possible to start a shell session in a running container (without ssh)

use this docker exec -it "id of running ...READ MORE

answered Jul 25, 2018 in Docker by Kalgi
• 35,720 points
21 views
+13 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Unalble to pull a new container after upgrading Helm.

If there are changes to roll out, ...READ MORE

answered Apr 12, 2018 in Docker by shubham
• 6,480 points
37 views
0 votes
1 answer

Enter in a Docker container already running with a new TTY?

Here is what you can try. For docker ...READ MORE

answered Oct 26, 2018 in Docker by shubham
• 6,480 points
48 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.