Docker Automatic Pull Private Repository

On Image Startup

vITraining Admin

Here is how to make your Docker image automatically pull GitHub or BitBucket repository on starting up...

On your Github: create OAuth Token.

Go to

Generate new token and save it.

On your host working folder where you build the image, clone the repository, for example I put it on addons folder:

git config --global credential.helper store
cd addons
git clone REPOURL
Username: youraccount
Password: yourtoken

Git credential file will be generated on ~/.git-credentials containing your account and token to be copied to the Image.

Copy it to your host working folder:

cp ~/.git-credentials .

On you Dockerfile:

Install sudo and git  along with other packages required by the image.

RUN set -x; apt-get update && apt-get install -y --no-install-recommends git  sudo 

Copy local repo onto image, example :

COPY ./addons /opt/addons

Setup git account inside the container.

RUN git config --global credential.helper store
COPY ./.git-credentials /root/.git-credentials

Setup sudo user, eg odoo user, so that it can do sudo inside the container later:

RUN adduser odoo sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

Done editing Dockerfile.

Now on, put the pull command at the container local repository folder :

sudo git -C /opt/addons pull

Build the image as usual.

Now, every time you start the image, it will pull the latest version of your code from the repository!


Need more trick on docker ? 

See my Udemy course: