Automate Building & Pushing Docker Containers Using GitHub Actions
Tags: devops, docker, ci, github
Published: 2019-06-20 13:32:45

---
title: Automate Building & Pushing Docker Containers Using GitHub Actions
published: true
description: Want to know how to use GitHub Actions to automate building & pushing containers to Docker Hub?
tags: devops, docker, ci, github
cover_image: https://thepracticaldev.s3.amazonaws.com/i/yyd448aszddi6awfwn56.png
---

Welcome to my second article! It took a while, but I hope you will enjoy this one.

You might think, "Docker containers? What are those?" My apologies, but I won't explain what containers are in this article. That said, I was thinking about writing a beginner friendly series about containers and serverless computing. So if you would be interested in such a thing, please let me know!

Today I'm gonna walk you through on how to automatically build and push containers defined in your GitHub repository to your [Docker Hub](https://hub.docker.com/) account using [GitHub Actions](https://github.com/features/actions).

Using this method, you can keep your Docker Hub images in sync with your repository. Every time you push to the repository, the container will be built and pushed to the Docker Hub.

# Let's bake a cake
To make this all a bit easier to follow, we're going to pretend we are reading a recipe. I will first list all the ingredients (requirements) and then I will explain the process using more detailed steps.

# Ingredients
(Also known as *requirements*.)
####You will need:
- A GitHub account that has access to GitHub Actions (you can request access [here](https://github.com/features/actions))
- A repository that has access to GitHub Actions
- A valid `Dockerfile` in your repository
- A Docker Hub account

# Let's automate!
Got all the ingredients? Awesome!
Let's use GitHub Actions to automate building and pushing your container to Docker Hub.

First, navigate to your repository and click on `Actions` at the top.
You'll end up in an interface similar to this one. Create a new workflow file.
![GitHub Actions create a new workflow](https://thepracticaldev.s3.amazonaws.com/i/usv1mepsruth8ck1vgfc.png)

We'll create a workflow that runs when someone pushes something to the repository.
![GitHub Actions configure workflow](https://thepracticaldev.s3.amazonaws.com/i/hhrs14vzbkdk7jv9dhaa.png)

Drag the blue connector down and add the 'Docker Registry' action.
![GitHub Actions add Docker Registry action](https://thepracticaldev.s3.amazonaws.com/i/sv27untseku6y9hunbxy.png)

This action will log you in to the Docker Hub using the `DOCKER_USERNAME` and `DOCKER_PASSWORD` secrets. Add your username and password as a secret. Be careful! Don't add them as an environment variable, otherwise they will be visible!
![Adding secrets to the Docker Registry action](https://thepracticaldev.s3.amazonaws.com/i/aismw9sxl7nmfbjzzfrk.png)

Next up, add another action below the 'Docker Registry' action. This time we will use the 'GitHub Actions for Docker' action. Using this action you are able to run Docker commands like you would in your terminal.
The command you want to run needs to go into the `args` field. The action will automatically put `docker` in front of your command, so make sure you don't.

Let's pretend we would build and push our container in the terminal for a second.

Before we are able to push the container we need to build it first. To do this we would use the command `docker build -t [username]/[container name] [path to Dockerfile]`
For my [example repository](https://github.com/sven-zo/actions-docker-hub-demo) this would be: `docker run build -t svenzo/actions-docker-hub-demo src`. If your Dockerfile is in the root of the repository, you can put a `.` (dot) instead of the folder path.

The second command we would run is the push command: `docker push [username]/[container name]`. Once again, for my [example repository](https://github.com/sven-zo/actions-docker-hub-demo) this will be as follows: `docker push svenzo/actions-docker-hub-demo`.

To make these commands work in the `args` field we need to remove the first `docker` in front of the command, and combine both commands using `&&`. You will end up with something like this: `build -t [username]/[container name] [path to Dockerfile] && docker push [username]/[container name]`.
For my [example repository](https://github.com/sven-zo/actions-docker-hub-demo) this will be: `build -t svenzo/actions-docker-hub-demo src && docker push svenzo/actions-docker-hub-demo`.
![GitHub Actions for Docker](https://thepracticaldev.s3.amazonaws.com/i/dgcnyknr6v7vfrz8pa1b.png)

Finally, commit the workflow file using the menu at the top right.
![Commit](https://thepracticaldev.s3.amazonaws.com/i/7r6xq407shlpfz8wx4yn.png)

If you wait a few seconds and navigate to the `Actions` tab, you will see your actions running. If everything goes well, the container in your repository will be pushed to your Docker Hub account. If something went wrong, you will be able to look into the logs to check what went wrong.
![GitHub Actions success](https://thepracticaldev.s3.amazonaws.com/i/uxj6ch6cgt4ajty9i3wp.png)

You can navigate to your Docker Hub account and see the container image GitHub Actions pushed for you.
![Docker Hub success](https://thepracticaldev.s3.amazonaws.com/i/0n8lh0ye3mz60ih3s1si.png)

# Final thoughts
Hey, did you enjoy reading this article? I hope you found this tutorial useful. Please let me know what you think in the comments. Additionally, I was thinking  on writing a beginner friendly series of articles on the subject of 'containers and serverless'. Would you be interested? Please let me know as well!

Anyways, [here's a song you might like](https://song.link/i/284000387).

See you soon, and take care ✌
~ Sven

-------
# Attribution
Banner image background by [Kyle Ryan](https://unsplash.com/photos/pSyfecRCBQA) on Unsplash
GitHub icon made by [Icomoon](https://www.flaticon.com/authors/icomoon) from [Flaticon](https://www.flaticon.com/)
Docker icon made by [Icons8](https://icons8.com)

-------
Thanks for reading! Leave a like ❤ or a comment 🖊 if you want to. And most importantly; don't give up if GitHub Actions isn't working like you'd want to!