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!
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.
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.
(Also known as requirements.)
- A GitHub account that has access to GitHub Actions (you can request access here)
- A repository that has access to GitHub Actions
- A valid
Dockerfilein your repository
- A Docker Hub account
Got all the ingredients? Awesome!
Let's use GitHub Actions to automate building and pushing your container to Docker Hub.
This action will log you in to the Docker Hub using the
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!
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.
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.
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 ✌
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!