Photo by Ian Taylor on Unsplash.
Do you want to write a Golang application that uses Docker containerization? The purpose of this article is to help you quickly containerize your Golang application for development (with hot loading) and production purposes.
Please install Docker Desktop before continuing. Once installed, launch the desktop application and if it runs successfully, you’re good to go.
Also, I’m assuming you have a $GOPATH directory where you can put the Golang source code you’re working on. For example, mine is: ~/go/src/github.com/bartmika.
Don’t worry! Docker is a full-fledged toolset that has been around long enough to provide a wealth of excellent teaching resources to help you learn. I recommend the following because it helps me a lot:
Check out another article I wrote that can help you learn the basics
Definitely a Docker learning resource for beginners in Golang programming
In this section, you will learn how to set up your Golang application on your machine for local development. The purpose of developing containers is to hold all dependencies (e.g., third-party packages such as GORM), infrastructure (e.g., databases, memory caches, etc.), and code that helps and improves your development.
Create a repository for our application
Each time you want to increase a dependency, you can close the currently running container and install the dependency. Install our dependencies as follows:
Dockerfile is great when you want to package your application into a container for deployment. Not very suitable for developers who want to put the source code outside the container and those that want the running container to react to changes to the source code.
So essentially, if you don’t have a problem with this restriction, go ahead!
What is the .dockerignore file? Essentially, it is similar to .gitignore where some files/folders are not saved to docker containers and images.
Create a dev. Dockerfile file and copy and paste the following into it.
Create our dev.docker-compose.yml file:
Wait, why do I use dev for these Docker filenames. Prefix? The reason is because I want to distinguish between Docker files for production and development purposes.
In your terminal, start the development environment with the following command:
Confirm that we can access it.
You will see the following output in your terminal:
The purpose of this section is to make your Golang application containerized and ready to run in production.
The simplest setup is a single-stage build. The problem with building this way is that your Docker image will be very large. First, create a Dockerfile in your project root folder and copy and paste the following into it:
A more complex setup called a multi-stage build can save disk space. The main idea is that you build your Golang application in one container and then move it to another, smaller container, discarding a container with a lot of disk space.
If you want to give it a try, create a Dockerfile in the project root folder and copy and paste the following into it:
Then run the up command and you will see it work:
Proofreader: Liu Sijia
For information such as conference cooperation, on-site recruitment and enterprise ticket purchase, please contact WeChat: 18516100522
Poke here GO!