How Docker Transforms Go Web App Development and Deployment
This tutorial explains how to containerize a simple Go web application with Docker, set up a development workflow using Docker and Bee, build and run images, and automate continuous integration and deployment with Semaphore, covering prerequisites, Dockerfile creation, testing, and production deployment steps.
Introduction
Most Go applications compile to a single binary, but web applications often include templates and configuration files, which can cause synchronization issues when many files are involved. This tutorial shows how Docker can improve the build, test, and deployment workflow for Go web apps, benefiting teams of any size.
Objectives
Gain a basic understanding of Docker.
See how Docker helps develop Go applications.
Create Docker containers for a production Go app.
Use Semaphore for continuous deployment.
Prerequisites
Docker installed on host or server.
A server that accepts SSH key authentication.
Understanding Docker
Docker packages an application and all its dependencies—code, runtime, system tools, and libraries—into a container, ensuring the same environment everywhere. Containers are lightweight compared to virtual machines because they share the host kernel, start faster, and use fewer resources.
Why Run a Go Web App in Docker?
Even though Go binaries are simple, Docker keeps templates and configuration files synchronized with the binary, guarantees identical environments in development and production, and eliminates “works on my machine” problems, especially in large teams with diverse host setups.
Creating a Simple Go Web Application
The example app, MathApp , provides routes for addition and multiplication, uses Beego for routing and HTML templates, and includes a configuration file.
<code>MathApp
├── conf
│ └── app.conf
├── main.go
├── main_test.go
└── views
├── invalid-route.html
└── result.html
</code>main.go defines routes, extracts parameters, performs calculations, and renders
result.html. main_test.go contains unit tests for the add and multiply functions.
Development Dockerfile
<code>FROM golang:1.6
# Install Beego and Bee
go get github.com/astaxie/beego && go get github.com/beego/bee
EXPOSE 8080
CMD ["bee", "run"]
</code>This image installs the Go toolchain, Beego, and the Bee live‑reload tool, exposing port 8080.
Building and Running the Development Container
<code>docker build -t ma-image .
docker run -it --rm --name ma-instance -p 8080:8080 \
-v /app/MathApp:/go/src/MathApp -w /go/src/MathApp ma-image
</code>The
-vflag mounts the project directory so changes are reflected instantly. Bee watches for file changes, rebuilds, and restarts the app automatically.
Production Dockerfile
<code>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/MathApp
</code>This Dockerfile copies the compiled binary and required files into a clean image, exposing port 8080.
Continuous Integration with Semaphore
Semaphore automates the pipeline:
On git push, pull the repository and run
go getand
go build.
Run unit tests.
If successful, build a Docker image, tag it, and push to Docker Hub.
SSH into the server and execute an update script.
Update Script (update.sh)
<code>#!/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) || :
</code>The script pulls the latest image, stops the old container, starts a new one, and cleans up dangling images.
Deploy Commands in Semaphore
<code>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_user@your_ip "~/update.sh $DH_USERNAME"
</code>These commands compile the Go binary, build and push the Docker image, and trigger the remote update.
Verification
After deployment, accessing
http://your_ip:8080/sum/4/5displays the calculated result, confirming that both development and production pipelines work correctly.
Conclusion
The tutorial demonstrates how Docker streamlines Go web application development, testing, and deployment, and how Semaphore provides a fully automated CI/CD workflow, enabling reliable and repeatable releases.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.