How Docker Transforms Go Web App Development and Continuous Deployment
This tutorial shows how to containerize a Go web application with Docker, use Beego for routing, set up development and production Dockerfiles, run automated tests, and integrate Semaphore for continuous integration and deployment, enabling consistent environments and streamlined workflows.
Introduction
Learn how Docker can improve the way you build, test, and deploy Go web applications, and understand how to use Semaphore for continuous deployment.
Goals
After completing this tutorial you will have a basic understanding of Docker, see how Docker helps Go development, create Docker containers for a production Go app, and know how to continuously deploy Docker containers with Semaphore.
Prerequisites
Docker installed on your host or server, and a server that accepts SSH key authentication.
Understanding Docker
Docker packages an application and its dependencies into a container, ensuring the same environment across development and production, eliminating issues caused by missing files or environment differences.
Advantages Over Virtual Machines
Containers share the host OS kernel, making them lightweight, faster to start, and requiring fewer resources than virtual machines.
Docker in Development
Docker provides a standard development environment for all team members, centralizes dependencies, and ensures the development and production environments are identical.
Why Use Docker for a Go Web App?
Docker keeps templates and configuration files in sync with the binary, guarantees identical configurations in development and production, and standardizes environments across diverse host systems.
Create a Simple Go Web Application
The tutorial builds a simple MathApp with routes for sum and product operations, using the Beego framework.
Final Directory Structure
MathApp
├── conf
│ └── app.conf
├── main.go
├── main_test.go
└── views
├── invalid-route.html
└── result.htmlApplication Files
main.go contains the application logic:
package main
import (
"strconv"
"github.com/astaxie/beego"
)
type mainController struct {
beego.Controller
}
func (c *mainController) Get() {
operation := c.Ctx.Input.Param(":operation")
num1, _ := strconv.Atoi(c.Ctx.Input.Param(":num1"))
num2, _ := strconv.Atoi(c.Ctx.Input.Param(":num2"))
c.Data["operation"] = operation
c.Data["num1"] = num1
c.Data["num2"] = num2
c.TplName = "result.html"
switch operation {
case "sum":
c.Data["result"] = add(num1, num2)
case "product":
c.Data["result"] = multiply(num1, num2)
default:
c.TplName = "invalid-route.html"
}
}
func add(n1, n2 int) int { return n1 + n2 }
func multiply(n1, n2 int) int { return n1 * n2 }main_test.go provides unit tests:
package main
import "testing"
func TestSum(t *testing.T) {
if add(2, 5) != 7 { t.Fail() }
if add(2, 100) != 102 { t.Fail() }
if add(222, 100) != 322 { t.Fail() }
}
func TestProduct(t *testing.T) {
if multiply(2, 5) != 10 { t.Fail() }
if multiply(2, 100) != 200 { t.Fail() }
if multiply(222, 3) != 666 { t.Fail() }
}View Files
result.html displays calculation results:
<!doctype html>
<html>
<head><title>MathApp - {{.operation}}</title></head>
<body>
The {{.operation}} of {{.num1}} and {{.num2}} is {{.result}}
</body>
</html>invalid-route.html shows an error for unknown operations:
<!doctype html>
<html>
<head><title>MathApp</title><meta name="viewport" content="width=device-width, initial-scale=1"><meta charset="UTF-8"></head>
<body>Invalid operation</body>
</html>Configuration File
; app.conf
appname = MathApp
httpport = 8080
runmode = devUsing Docker in Development
Create a development Dockerfile:
FROM golang:1.6
RUN go get github.com/astaxie/beego && go get github.com/beego/bee
EXPOSE 8080
CMD ["bee", "run"]Build the image:
docker build -t ma-image .Run the container with live reload:
docker run -it --rm --name ma-instance -p 8080:8080 \
-v /app/MathApp:/go/src/MathApp -w /go/src/MathApp ma-imageModify source files and see automatic rebuilds and restarts.
Production Dockerfile
FROM golang:1.6
RUN mkdir /app
ADD MathApp /app/MathApp
ADD views /app/views
ADD conf /app/conf
WORKDIR /app
EXPOSE 8080
ENTRYPOINT /app/MathAppContinuous Integration with Semaphore
Configure Semaphore to run go get, go build, login to Docker Hub, build and push the Docker image, then SSH into the server to execute an update script.
go get -v -d ./
go build -v -o MathApp
docker login -u $DH_USERNAME -p $DH_PASSWORD -e $DH_EMAIL
docker build -t ma-prod .
docker tag ma-prod:latest $DH_USERNAME/ma-prod:latest
docker push $DH_USERNAME/ma-prod:latest
ssh -oStrictHostKeyChecking=no your_server_username@your_ip_address "~/update.sh $DH_USERNAME"The update.sh script pulls the latest image, stops the old container, runs a new one, and cleans up dangling images.
#!/bin/bash
docker pull $1/ma-prod:latest
if docker stop ma-app; then docker rm ma-app; fi
docker run -d -p 8080:8080 --name ma-app $1/ma-prod
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)Deployment
After pushing changes to the repository, Semaphore automatically builds, tests, and deploys the updated container. You can verify the deployment by visiting http://your_ip_address:8080/sum/4/5.
Summary
This tutorial demonstrated how to containerize a Go application with Docker, set up development and production Dockerfiles, run automated tests, and use Semaphore for continuous integration and deployment, simplifying future Go app deployment tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
