How Docker Transforms Go Web App Development and Deployment
This tutorial explains how Docker can streamline building, testing, and deploying Go web applications, introduces the use of Semaphore for continuous deployment, and walks through creating development and production Dockerfiles, configuring CI/CD pipelines, and managing updates on the server.
Introduction
Familiarize yourself with how Docker can improve the way you build, test, and deploy Go web applications, and understand how to use Semaphore for continuous deployment.
Goals
Gain a basic understanding of Docker.
Discover how Docker can help you develop Go applications.
Learn how to create Docker containers for a Go application in production.
Know how to continuously deploy Docker containers on your server using Semaphore.
Prerequisites
Docker installed on your host or server.
A server that can authenticate SSH requests with an SSH key pair.
Understanding Docker
Docker helps you create a self‑contained deployable unit called a container, which bundles the application code (or binary), runtime, system tools, and libraries, ensuring the same environment everywhere and eliminating many synchronization and environment‑difference problems.
Advantages over Virtual Machines
Containers share the host kernel, making them lighter and faster to start than virtual machines, which require a full guest OS.
Docker Advantages in Development
A standard development environment for all team members.
Centralized dependencies that work the same everywhere.
Exactly the same environment in development and production.
Catch bugs that only appear in production early.
Why Run a Go Web Application with Docker?
Most Go applications are simple binaries, but web apps include templates and config files that need to stay in sync. Docker ensures the same configuration in development and production, provides a consistent environment for large teams, and eliminates many deployment‑related issues.
Creating a Simple Go Web Application
We will build a small Go web app called MathApp that demonstrates different arithmetic operations, uses HTML templates, a configurable file, and includes tests.
Final Directory Structure
MathApp
├── conf
│ └── app.conf
├── main.go
├── main_test.go
└── views
├── invalid-route.html
└── result.htmlApplication File Contents
The main Go file contains the routing and controller logic.
*// main.go*
package main
import (
"strconv"
"github.com/astaxie/beego"
)
func main() {
// beego.Router(":operation/:num1:int/:num2:int", &mainController{})
// beego.Run()
}
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 }Test File Contents
// main_test.go
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 File Contents
Result template (result.html):
<!-- result.html -->
<!doctype html>
<html>
<head><title>MathApp - {{.operation}}</title></head>
<body>
The {{.operation}} of {{.num1}} and {{.num2}} is {{.result}}
</body>
</html>Invalid route template (invalid-route.html):
<!-- invalid-route.html -->
<!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 Contents
; app.conf
appname = MathApp
httpport = 8080
runmode = devUsing Docker in Development
We create a development Dockerfile that installs Beego and the bee tool, exposes port 8080, and runs bee run for live reloading.
FROM golang:1.6
# Install beego and the bee dev tool
RUN go get github.com/astaxie/beego && go get github.com/beego/bee
# Expose the application on port 8080
EXPOSE 8080
# Set the entry point to the bee command that runs the application and watches for changes
CMD ["bee", "run"]Build the image: docker build -t ma-image . Run the container with source mounting and port mapping:
docker run -it --rm --name ma-instance -p 8080:8080 \
-v /app/MathApp:/go/src/MathApp -w /go/src/MathApp ma-imageDuring development, any change to main.go triggers an automatic rebuild and the server restarts, as shown in the console output.
Using Docker in Production
We set up a CI/CD pipeline with Semaphore to automatically compile, test, build a Docker image, push it to Docker Hub, and deploy it on a remote server.
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/MathAppUpdate Script (update.sh)
#!/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
if docker rmi $(docker images --filter "dangling=true" -q --no-trunc); then :; fiCI/CD Commands in Semaphore
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"Conclusion
In this tutorial we learned how to containerize a Go web application with Docker, set up continuous integration and deployment using Semaphore, and automate production updates, simplifying future Go application deployments.
Link: https://www.cnblogs.com/zhangboyu/p/7452751.html
(Copyright belongs to the original author, please delete if infringing)
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
