yellowcode
Published in

yellowcode

How To Automate Docker Builds With GitHub

Create a NodeJS app pushed to a GitHub repository and have it automatically build on Docker Hub

How To Automate Docker Builds With GitHub

Why Automated Builds

If you’re familiar with managing the repository for your code and its Dockerfile, you’ll know that the process for deployment involved managing two “Hubs” (GitHub and DockerHub) and packaging your code into a Docker Image manually, through commits and pushes.

Docker Hub Automated Builds Announcement

Docker Hub introduced this feature a little while back, but I’m going to dive into experimenting with it by building a simple NodeJS application.

Key Benefits

Some key benefits to this process are that it can speed up your build process, essentially eliminating some steps, and all you need to manage is your repository.

Although this can speed up things, it might be a bit harder to implement with more dependent and complex micro services.

New Repository & Code Setup

Let’s begin by creating a new GitHub repository and creating our simple NodeJS application.

Creating a new GitHub repository
git clone https://github.com/codingwithmanny/deploy-nodejs-docker;
cd deploy-nodejs-docker;
yarn init;

Adding Dependencies

yarn add express;

Our Code

index.js

const express = require('express');const app = express();
const port = 80;
app.get('/', (req, res) => res.send({ hello: 'world' }));app.listen(port, () => console.log(`Listening on port: ${port}`));

Running our code we get:

node index.js;
# Listening on port: 80
NodeJS Running On Port 80

Adding Git Ignore

touch .gitignore;
nano .gitignore;

.gitignore

node_modules/*

Pushing our code to our new repository:

git add .;
git commit -m "Feat: Initial application commit";
git push origin master;

Linking To GitHub Repository

Let’s start by creating a new Docker Hub repository and linking our existing GitHub repository of our simple NodeJS application.

Creating New Repository On Docker Hub

After we’ve created the repository, we need to connect it with GitHub and point it to our new repository.

Link Docker Hub Repository To GitHub
Pointing To GitHub Repository

You’ll see that it will start to automatically start building the repository but it’ll fail because there isn’t Dockerfile for it to reference and build with, which is our next step.

Docker Trying To Build But Will Fail

Creating Our Dockerfile

Referencing our our article on Building A Simple NodeJS Application with Docker, we’ll borrow the configuration of the Dockerfile.

Dockerfile

# base image
FROM alpine
# port exposed
EXPOSE 80
# copy current directory into /mnt
COPY . /mnt
# install dependencies
RUN apk update && \
apk add nodejs && \
apk add npm && \
apk add build-base && \
apk add python && \
cd mnt && \
npm install && \
apk del build-base && \
apk del python && \
apk del npm;
# command executed at run
CMD ["/bin/sh", "-c", "cd /mnt; node index.js;"]

Let’s save this in the root and push to git:

git add .;
git commit -m "Feat: Added Dockerfile";
git push origin master;

Verifying Docker Hub

After we’ve pushed the code, it will automatically check the repository for the Dockerfile and start building the image. It might take a bit of time for the first time, so it might require a little bit of patience.

Docker Hub Successful Build

Running Our New Docker Image Locally

Now that our image is built, let’s test the application locally but on a different port to show a difference for this tutorial.

docker run -it -d -p 9000:80 --name testnodejs codingwithmanny/deploy-nodejs-docker;

Load up http://localhost:9000 and there you go:

Docker NodeJS Application Running Locally

Deployment

There could be a process to automate the deployment as well, but for now, I’m just going to demonstrate the app being deployed on Digital Ocean (referral link).

You’ll need to make sure to have two things setup correctly for the new droplet.

Docker Instance

Make sure you use the Docker Instance from the Marketplace.

Docker Instance From Digital Ocean

User Data

Set the user data settings to automatically start your Docker container as soon as the droplet is done being setup.

Now we have our new Docker image up and running on Digital Ocean.

New Droplet Setup
Our NodeJS Deployed With Digital Ocean

Final Thoughts

Docker Hub’s Automated Build feature is definitely something I’ll be exploring more. This is a first step, but there are other things I would like to see: automated tests, environment variables, and even an automated deployment process setup.

If you got value from this, please share it on twitter 🐦 or other social media platforms. Thanks again for reading. 🙏

Please also follow me on twitter: @codingwithmanny and instagram at @codingwithmanny.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manny

Manny

DevRel Engineer @ Polygon & Web Application / Full Stack Developer