DEV Community

Cover image for CI/CD pipeline with Docker, Github actions, Dockerhub and Watchtower

Posted on

CI/CD pipeline with Docker, Github actions, Dockerhub and Watchtower

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.

To "dockerize" an app, you write a Dockerfile. Here's an example of a dockerfile.

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.

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.

Below is an example actions.yml file to build an image and push the image to the Dockerhub.

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 logo containrrr / watchtower

A process for automating Docker container base image updates.

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.

Top comments (2)

starpebble profile image

It's helpful! Here's what I'm thinking: the aws-cli is available as a docker container. Yes, it's available as a local install, there are benefits to the docker container. So I use the container. It's an open-source tool in a GitHub repository. Like a ci/cd for an open-source GitHub repository. Watchtower is interesting, possibly it can look for the latest version of tools and auto-update the aws-cli container with the new version on the local machine.

tsuki42 profile image

Watchtower regularly checks for the updates in the registry and, if updates are available, automatically downloads the latest image and restarts the container with the same commands as were previously used.