Skip to content

Instantly share code, notes, and snippets.

@hobbsAU
Last active September 21, 2017 06:13
Show Gist options
  • Save hobbsAU/b6a17108dfa8898469d2 to your computer and use it in GitHub Desktop.
Save hobbsAU/b6a17108dfa8898469d2 to your computer and use it in GitHub Desktop.

Description

GIT and Docker can be used together to manage and automate the building and updating of Docker containers.

I will walk through the setup and creation of a simple git project to create and maintain a docker container.

I will use Arch linux as the host for both the git project development and the docker run host.

Installation

File Locations

Configuration - git repository

  1. Firstly let's setup your GitHub account and use SSH public key for authentication. This will allow us to easily push and pull over ssh. Instructions on how to setup SSH automate login to GIT can be found here: https://help.github.com/articles/generating-an-ssh-key/#platform-mac

  2. Test the SSH conection

  3. OK following the instructions here https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ we will setup a new git project, add some files and then publish to a public repository on github. First let's setup the project:

$ cd && mkdir src
$ git init docker-tvheadend
$ cd docker-tvheadend
  1. Let's create two files 1) Dockerfile and 2) README
$ vi Dockerfile
$ vi README
  1. Let's add the files to the new local repository. This will stage for the first commit. Let's then create and initial commit for the new files.
$ git add Dockerfile README.md
$ git commit -m 'Initial project'        
[master (root-commit) 9ad5bb8] Initial project
2 files changed, 92 insertions(+)
create mode 100644 Dockerfile
create mode 100644 README.md
  1. Go to github and create a new repository copying the location for the respository from the quick setup page. Let's add the remote github repository:
$ git remote add origin [email protected]:hobbsAU/docker-tvheadend.git
$ git remote -v
origin	[email protected]:hobbsAU/docker-tvheadend.git (fetch)
origin	[email protected]:hobbsAU/docker-tvheadend.git (push)
  1. Push the changes from your local repository to github:
$ git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.16 KiB | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To [email protected]:hobbsAU/docker-tvheadend.git
 * [new branch]      master -> master
  1. If you make changes to any of the files you may wish to update your online respository like this:
$ vi README
$ git commit -a
[master 4edb789] changed README
1 file changed, 9 insertions(+), 1 deletion(-)
$ git push origin master

Configuration - dockerhub autobuild

  1. Let's now setup your DockerHub account and cache the login credentials. Using the credentials (username, password and email address) that you signed up to Docker with execute the following:
$ docker login
Username: <yourlogin>
Password: 
Email: <youremail>
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
  1. Docker provides a quick and easy way to connect your DockerHub account with your Github account and then create a simple automated build process.

Please take a moment to read through this article from Docker detailing the steps to link accounts, security precautions and other information. The basic steps are outlined here:

  • a. Connect your Docker Hub and GitHub accounts. This allows Docker to watch for changes to the code repo on GitHub and cause actions to occur in the corresponding image repo on the Docker Hub.
  • b. Select the GitHub repo you would like to connect to the Docker Hub. This will create a new image repo in your Docker Registry namespace the corresponds to the code repo in GitHub.
  • c. You will see a new build pending in your image repo. Wait until it is done.

Now let's test out the new image!

$ sudo docker pull hobbsau/tvheadend
$ sudo docker create \
 --name tvheadend-data \
 -v /srv/tvheadend/config:/config \
 -v /srv/tvheadend/recordings:/recordings \
 hobbsau/tvheadend
$ sudo docker run -d \
 --restart=always \
 --net="host" \
 --volumes-from tvheadend-data \
 --name tvheadend-service \
 hobbsau/tvheadend
  1. You can force a rebuildof the image by setting up a remote build trigger with instructions here
curl --data build=true -X POST https://registry.hub.docker.com/u/hobbsau/tvheadend/trigger/<insert trigger token>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment