CI/CD pipeline with Docker, Github actions, Dockerhub and Watchtower
Tags: docker, github, watchtower, devops
Published: 2020-08-13 05:21:49

A CI/CD pipeline for the development phase.

I wanted to setup a CI/CD pipeline where my application would finally be deployed on my local machine while also using the sweet stuff that github provided with github-actions, _local-machine_ being the key here.

There can be many situations where you want the deployment to be local, particularly during the development phase, to:
 - prevent network traffic
 - non-transmission of sensitive data
 - accommodate frequent changes in the code-base

##### *Note:* This article requires you to have the atleast some prior knowledge of docker and github-actions, and docker installed on your local machine.

---

### 1. Dockerizing your application
"Docker is an open platform for developing, shipping, and running applications"
Docker is like a VM but with only the parts necessary to run your applications. Docker provides a comprehensive guide on developing with docker in their documentation, which can be found [here](https://docs.docker.com/develop/).

To "dockerize" an app, you write a Dockerfile. Here's an example of a dockerfile.
{% gist https://gist.github.com/tsuki42/0309cd58586cc87316d10b936bdf9fa8 %}

### 2. Setting up github actions for testing and CI
Once you have the dockerfile, you can build a docker image. A Docker image is a file, comprised of multiple layers, that is used to execute code in a Docker container. A docker image is basically a _packaged application_ that can be executed as _docker containers_. A docker container is just a running docker image.

Github actions provides an environment for executing "actions", which can perform various tasks. These actions are written as _yaml files_, which then execute based on github triggers like _pushing to a branch_, _initiating a pull-request_ etc. Check out the features of github actions [here](https://github.com/features/actions).

Dockerhub is an online registry where you can keep your docker images. You do require a dockerhub account to docker images up the registry. If you prefer, you can also make you own registry instead. More on this [here](https://docs.docker.com/registry/deploying/).

Below is an example _actions.yml_ file to build an image and push the image to the Dockerhub.
{% gist https://gist.github.com/tsuki42/e0cdff4e94a8f7df955002a2bb84e558 %}
The example above uses a custom script `ci:docker` to build the docker-image, login into the dockerhub and push the image to the dockerhub using cli. The example is for a node app, but a similar approach can be used for any application.

### 3. Watchtower
The center part of this entire pipeline is *watchtower*.

"With watchtower you can update the running version of your containerized app simply by pushing a new image to the Docker Hub or your own image registry."
Check out their repo for  more information.
{% github containrrr/watchtower no-readme %}

Basically, with watchtower, whenever the image updates in the registry (like Dockerhub), the container is updated with the latest changes.

---

Doing these, you have successfully made your CI/CD pipeline which gets updated whenever changes are pushed to the github repo.
The local container takes some time to update once the updated image is pushed to the registry, but setting these whole workflow is simple and works beautifully, at least for the development environment.