Docker is the most popular containerization technology owing to its simplicity and ease of use. Docker relieves the stress of portability issues in software development and distribution. you’re able to deploy your docker containers to most cloud service providers.
Containerizing your Go apps with Docker can help you ensure consistent and reliable deployment across different environments. you’re able to deploy your Go apps to different environments like development, staging, and production. Docker containers are lightweight and take up less space than traditional virtual machines. This can save you money on hosting costs, and it can also make your deployments faster.

Setting Up a Simple Web Server in Go
The Go standard library contains the packages you’ll need to set up a simple web server.
First, import thehttp,log, andjsonpackages. You’ll useGo’s http packageto set up the server andGETrequest endpoint. Thelogpackage for logging possible errors to your console. Thejson package for encoding a struct to JSONfor the API endpoint.

you’re able to encode a struct instance as JSON to the client as a response based on the validity of the request as thus:
The handler function would return a successful message to the client if the request to the endpoint is aGETrequest.
You set up the handler function in the main function with the route as/api/docker/go. ThedockerTestEndpointhandler function validates that the request to the handler is a GET request. If it’s a GET request, it encodes an instantiatedMessagestruct instance to the client based on the request’s status.
Here’s how you’re able to mount the handler function on a route and setup the server to run on port8080:
Themainfunction is the server’s entry point, which listens on port8080. TheHandleFuncmethod mounts the routes on the handler function. TheListenAndServemethod starts the server on the specified local host port8080.
Getting Started Containerizing Your Go Apps With Docker
After installing and setting up Docker, you’ll need a Docker file named Dockerfile to create and build a Docker image for your Go app. You’ll specify commands for the base image and commands to copy the files, add the working directory, and run the app in the Dockerfile.
Run this command in the terminal of your workspace to create a Dockerfile.
You’ll specify the commands for building your Docker image in the Dockerfile.
If there are any files you want to separate from your Docker image, you’re able to use a.dockerignorefile. The.dockerignorefiles work exactly like.gitignorefiles.
Next, you’ll specify build commands in your Dockerfile to containerize your apps.
Defining Commands in the Dockerfile
Dockerfiles are customizable based on your project’s specifications. You’ll define commands to build the base image for building the application.
Here’s an example of the contents of a Dockerfile that builds the web server above:
The Dockerfile usesgolang:latestbase image, to build the app after setting the working directory to/app.
The Dockerfile copies the files with theCOPYcommand and downloads dependencies with theRUNcommand.
The file specifies a build and run operation with theRUNcommand, then sets the command to run when the container starts with theCMDcommand.
Save the Dockerfile in the same directory as yourgo.modandmain.gofiles; then run this command to build a Docker image from this Dockerfile:
The above command will create a Docker image with the taggolangtutorial.You can run a container with this command:
The command maps port 8080 from the container to port 8080 on the localhost of the host machine. You can request the server running in the Docker container from the host machine.
Here’s the result fromsending the CURL request to the server, this time running on Docker:
You Can Use Docker Compose for Container Orchestration
Docker Compose is a tool that you can use to orchestrate (work with many) Docker containers. Docker Compose allows you to define a multi-container application in a single YAML file. You can run and manage the entire application with a single command.
You can use Docker Compose for deploying and managing complex containerized applications. Docker Compose simplifies management with automated and consistent deployments.