In the recent few years, Go has become one of the favourite options, when it comes to building lightweight, fast and scalable services. In this tutorial, I will be going through how to dockerize and deploy a Go service in the shortest time possible.
In this article we are going to use Heroku which is probably the most frictionless platform out there for deploying and hosting services and web apps, if you don't have yet an account at Heroku, please go on and create one, it's pretty straightforward and also free.
I. Heroku Project Creation.
- Now that your account has been created, go ahead and create a new Heroku project/app.
- You will be presented with the next screen to input your project infos.
- Finally let's install the Heroku cli :
If you are on Windows or any other platforms, you can download and install the binary from this link 👉 : https://devcenter.heroku.com/articles/heroku-cli.
II. The Mock Service.
Let's go ahead, setup and implement our Go mock service.
- Create project
- Next step, Installing dependencies, open the terminal in the project directory and run the 2 commands below, they will install all we need.
$ go get -u github.com/gin-gonic/gin
$ go get github.com/brianvoe/gofakeit/v5
- Create a main.go file and put in the code in the snipped below 👇
Run the project, entry point is inside main.go, and open http://localhost:8080, should return current date & time in ISO 8601. If you got that right then the setup was done successfully.
- Implementing the mock data generation function. Here we will be using the fakeit library to generate some random mock data for our API.
- After implementing the mock data function, the main.go file will look 👇
III. Dockerize The Service
III. 1. Dockerfile & Heroku Config
A Dockerfile is a text file that holds necessary commands for assembling a docker image, which is a blueprint or a template that contains all necessary information and instructions to run an instance of our app on a Docker compatible platform.
- Create a file named
Dockerfile
in the project directory and put in the instructions below 👇
- Add the
heroku.yaml
config file in the project directory.
The project directory should look something like the tree below 👇
.
├── Dockerfile
├── go.mod
├── go.sum
├── heroku.yml
└── main.go
III. 2. Deploy the service.
- First step here is to track our project with version control, preferably Git in our case.
$ git init
$ git add .
$ git commit -m "Initial commit"
- Now, let's login in heroku, type the command below, and we will be asked to type any key do continue and it should open the browser to log in.
$ heroku login
- Next, open https://dashboard.heroku.com/apps and select the project that we initially created in the first part of this article, and then click on the Deploy tab.
- Set git remote heroku and link our local project to the one we created on Heroku.
- Last step is deploying the project to Heroku, and it's basically just a single line.
$ git push heroku master
After deployment logs, it should output "Verifying deploy... done", which should mean our service was deployed successfully and it then displays a URL on which it got deployed to, in my case it's https://myapp-name-x.herokuapp.com/, this URL should be different depending on how you named your project.
Opening the link and we should have an output like this 👇, obviously the data will be different but the structure should be similar.
IV. Conclusion
🎉 Congrats ! We have just dockerized and deployed our service to Heroku.
I hope you learned something throughout this tutorial, If there's any question, feedback or suggestion, please contact me via Twitter or E Mail
V. Resources.
- Finalized project can be found here 👉 : https://github.com/eddywm/dockerize-go-service.
- https://docs.docker.com/
Ciao 👋