In this task, we’ll create GitHub Actions to automatically build and publish our API and Web application Docker images to GitHub Container Registry (GCR) whenever we push changes to our repository.
GitHub Container Registry (GCR) is a container registry that allows you to store and manage Docker images within GitHub. It integrates seamlessly with GitHub Actions, making it easy to build, publish, and share container images.
The advantage of using a container registry like GCR is that it provides a centralized location to store your Docker images, making it easier to manage and share them across different environments.
Before proceeding, ensure you have a GitHub repository set up for your project. Moreover, create a .github/workflows
directory in your repository root if it doesn’t already exist.
We’ll create two workflow files, one for the API and one for the Web application.
In the .github/workflows
directory, create a file named api.container.yml
and add the following content to it:
This workflow file specifies the following:
main
branch that include changes to the api
directory or the workflow file itself.api
directory to the root, logging in to GitHub Container Registry, building the Docker image, and pushing it to the registry.In the .github/workflows
directory, create a file named web.container.yml
and add the following content to it:
The workflow file for the Web application is similar to the API workflow but tailored for the Web application directory.
Once the workflows complete successfully, go to the Github repository page for your project.
Navigate to the “Packages” section in the sidebar. You should see your newly published Docker images for both the API and Web application.
If your repository is private, these packages will be private as well. You can click on each package, open “package settings,” and change its visibility to public if needed. For the purpose of this tutorial, I recommend making them public.
To verify that the images are working as expected, you can pull them locally and run them using Docker. Here’s how you can do it:
Go to project’s GitHub repository page.
Click on the “Packages” in the sidebar.
Click on the API package to open its details page.
At the top of the page, you’ll see the command to pull the image. Copy this command. For example, my command looks like this:
Open a terminal and run the command to pull the image.
Once the image is pulled, you can run it using the docker run
command:
You should see the API running locally at http://localhost:3000
. Try it using Postman.
Repeat the same steps for the Web application image.
You can now share these images with your team or deploy them to other environments. The only requirements are Docker and access to the GitHub Container Registry.
You’ve now set up GitHub Actions workflows that automatically build and publish your API and Web application Docker images to GitHub Container Registry. These workflows run independently when changes are pushed to their respective directories.
In the next task, we’ll create a Docker Compose configuration that uses these published images, allowing for easy deployment of your entire application.