DEV Community

Cover image for Automate Building & Pushing Docker Containers Using GitHub Actions
Sven Hoffmann
Sven Hoffmann

Posted on

Automate Building & Pushing Docker Containers Using GitHub Actions

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 account using GitHub 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)
  • 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

We'll create a workflow that runs when someone pushes something to the repository.
GitHub Actions configure workflow

Drag the blue connector down and add the 'Docker Registry' action.
GitHub Actions add Docker Registry action

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

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 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 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 this will be: build -t svenzo/actions-docker-hub-demo src && docker push svenzo/actions-docker-hub-demo.
GitHub Actions for Docker

Finally, commit the workflow file using the menu at the top right.
Commit

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

You can navigate to your Docker Hub account and see the container image GitHub Actions pushed for you.
Docker Hub success

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.

See you soon, and take care ✌
~ Sven


Attribution

Banner image background by Kyle Ryan on Unsplash
GitHub icon made by Icomoon from Flaticon
Docker icon made by Icons8


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!

Top comments (2)

Collapse
 
dxdevil profile image
dxdevil

@sven This tutorial doesnt work out anymore. The visual editor has been disabled.

Collapse
 
sven profile image
Sven Hoffmann

Oh, I didn't know! I'll see if I can make an updated version soon. Thanks for letting me know.