4

Is there a best practices for making the Dockerfile sensitive to the git branch?

Background:

I've noticed that the docker hub automated builds, also senses if there are several git branches, and makes an automated build for each of them when they are updated as below

enter image description here

This is a really nice feature, but it leaves me with a problem. My Dockerfile contains a specific call to check out from the git repository as

FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
RUN git clone https://bitbucket.org/user/myrepo.git
WORKDIR /myrepo
RUN make

This means that even though i have severl branches in the repository, all the the docker-hub tags will only pull the master branch.

So the question is: Is there a best practices for making the Dockerfile sensitive to the git hub branch?

Uggly solution:

I could ofclurse change the docker build file so that it pulls the correct branch for that tag, as

FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
RUN git -b my_branch clone https://bitbucket.org/user/myrepo.git
WORKDIR /myrepo
RUN make

but this would have the two main downsides

  1. I would need to manually add -b my_branch to the Dockerfile in every new branch where i wanted this to work.
  2. When the branch is merged back to master, that -b my_branch would also be merged and subsequently break the Dockerfile in the master branch.

This question talks about adding several docker-hub builds, but is not really what i need. Docker Hub Automated Build - Tagging

Thankful for suggestions

Community
  • 1
  • 1
Mikael Fremling
  • 720
  • 2
  • 8
  • 24

1 Answers1

1

You probably shouldn't be doing a git pull in the Dockerfile.

Following Infrastructure as Code practices, it makes sense to include your Dockerfile in the same repository as your code, so changes in one that require changes in the other are bundled together in the same commit. If this is the case, your application's source code is available any time you're building the Docker image, and you can copy it from disk instead of pulling it from GitHub.

Unfortunately, Docker's COPY directive doesn't allow navigating up the directory tree, even though symlinks or other tricks, so you'll need to manually create a copy of the repo inside the repo every time before you build the Docker image. That may look something like this:

[~/projects/my-app $]> rm -r docker/repo
[~/projects/my-app $]> cd ..
[~/projects $]> cp -r my-app repo
[~/projects $]> mv repo my-app/docker/
[~/projects $]> cd my-app
[~/projects $]> docker build docker

Your Dockerfile then will contain a COPY repo/ /myrepo instruction to copy it into the container.

I would recommend tagging your image with the git sha that you've copied into it.

When doing development, to save yourself from having to rebuild all the time, you can simply use docker run's -v option to mount your local repo on top of the version baked into the image.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • I must admit I don't really see how this helps. If I manually need to check out the correct branch and push it to the docker hub every time, it feels like it spoils the point of the automated docker builds. – Mikael Fremling Aug 14 '16 at 06:23
  • If you want to do this in your CI pipeline and are already doing Docker builds, it should be trivial to add this to your script, no? As I read your question, your problem is that your current setup requires substituting in values that change per-branch, whereas this is branch-agnostic and will work without needing to constantly change your dockerfile. – Xiong Chiamiov Aug 16 '16 at 00:58
  • 1
    ok sorry, I think I was confused before. Let's see if i got it right this time: You are saying that docker-hub does a complete pull of the correct branch, and then looks for a docker file from which it builds the docker image to be used later. This allows me to put `COPY` commands inside the Dockerfile that copies the (already existing) repository from outside into the container, instead of using an explicit `git pull -b `. Correct? – Mikael Fremling Aug 17 '16 at 09:18