In Golang Posted July 12th, 2021
All of the code you type to develop software is just one thing required to run it. On top of the translated code you write, you need libraries and a supporting environment to effectively run software.
Considering development with go, while it is true that much of all Go applications compile to a final binary file that could be run, there are certain cases such as web applications that also come with various assets, templates and configuration files.
If not managed correctly, all of these would easily fall out of sync and become a nightmare.
Containers are handy things that pack together all the needed entities and create a single identical environment for your application. This would highly help to maintain the same setups in development and production.
While you can create containers by yourself, there are services like Docker that lets you build containers easily. In this article, We will look at Docker and explain, with an example, how you can use Docker with Go lang applications.
As said earlier, Docker is a service which lets you easily create containers for your application that includes all the things that your application needs to execute like the binary output, runtimes, system tools and more.
The main advantages that Docker containers would have over virtual machines is that the resource allocation is way too less in containers than Virtual machines. This is because virtual machines are essentially another system with its own OS whereas Containers directly interact with Kernel as a general application.
So by including Docker containers in your Development, you would be able to have a standard development environment that can be used by all team members which provides you with a lot of advantages including easily being able to fix the problems in production deployment.
Before using Docker for Golang development, you need to have docker installed in your machine. The exact way for installing docker would depend on what machine you are developing the application. However, installing docker on all the machines is very easy! Just follow the general instructions given for your specific operating system.
After installing Docker, there are basically 3 steps of using it: Create the Docker file, Build the image and then run the container. Here, we assume that you already have a working Go application.
Let us discuss each of the steps briefly:
Docker file contains the basic configuration for the docker container. That information includes the base image of the container, the port of the working and all other important details.
While creating Docker file and specifying information, You need to make sure that certain requirements are met, like:
In the docker file, you would write a series of lines in the form of
Some of the important ones are:
FROM: In here, you need to input the base image. As you are building the container for Go application, you would use the Go base image.
RUN: if you use any tool, like bee, you can use this to run the command go get -u github.com/beego/bee to install the tool.
ENV & ARG: These let you set environment variables. ENV is used for defining run time variables and ARG is for build time variables.
EXPOSE: This lets you use the application for a specified port.
CMD: for specifying a particular command.
Once the docker configuration is created, it’s time to build the image.
In order to create an image, you need to use the docker build command and provide it with certain arguments.
Use –build-arg for providing build arguments. You can use -t flag for setting the tag name for the new image.
You can view all the images of docker using the command: docker images.
That’s it! Now you are all set and can run the container.
You need to use the docker run command to run the container you have set up. You can give it a lot of arguments including:
-it flag which specifies the container to start in interactive mode. -p flag which specifies port and more.
The docker now exposes your application on the specified port and you can see the output when you access it. Even inside the container, Bee is effortlessly able to hot reload code and rebuild your application automatically when there is a change.
You have seen a very basic way of using Docker for Go development. From here, you can build more complex docker configurations thus creating more complex containers!
Do you have a Project we can assist you with?
Use our Project Planner
We believe in integrating business and technology to greater heights through innovation and sheer creativity.
Discover Codism by reaching us at our offices in
New Jersey (USA)
230 N Maple Avenue Suite B-1-616 Marlton, NJ 08053 United States.
Phone: (973) 814-2525
Fax: (856) 988-7980