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

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
-b my_branch to the Dockerfile in every new branch where i wanted this to work.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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With