Cloud Native 8 min read

Build Custom Nginx and Go Application Docker Images from Scratch

This guide walks through two practical examples—creating a Docker image for Nginx with a custom HTML page and packaging a Go Gin‑based web service into a container—covering directory setup, Dockerfile creation, image building, container execution, and verification steps.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Build Custom Nginx and Go Application Docker Images from Scratch

Case 1: Build a Custom Nginx Image

Prepare a working directory and a simple index.html file that displays a custom greeting.

mkdir dockerfile
cd dockerfile
vim index.html

Sample index.html content:

<html>
  <head><title>page added to dockerfile</title></head>
  <body><h1>Hello, My Name is Xianchao, My wechat is luckylucky421302</h1></body>
</html>

Create the Dockerfile:

FROM rockylinux:8.9
MAINTAINER xianchao
RUN rm -rf /etc/yum.repos.d/*
COPY Centos-vault-8.5.2111.repo /etc/yum.repos.d/
RUN yum install wget -y
RUN yum install nginx -y
COPY index.html /usr/share/nginx/html/
EXPOSE 80
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]

The Dockerfile installs wget and nginx, replaces the default homepage with the custom index.html, and starts nginx in the foreground.

Build and verify the image:

docker build -t xianchao/nginx:v1 .
docker images | grep nginx

Run the container and check its status:

docker run -d -p 80 --name html2 xianchao/nginx:v1
docker ps | grep html2

Test the service: curl http://<em>HOST_IP</em>:80 The response should contain the custom HTML defined earlier.

Case 2: Package a Go Gin Application into a Docker Image

Install the Go toolchain: yum install go -y Create a new project directory and a main.go source file:

mkdir test
cd test
vim main.go

Sample main.go:

package main

import (
    "net/http"
    "github.com/gin-gonic/gin"
)

func statusOKHandler(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"status": "success~welcome to study"})
}

func versionHandler(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"version": "v1.1版本"})
}

func main() {
    router := gin.New()
    router.Use(gin.Recovery())
    router.GET("/", statusOKHandler)
    router.GET("/version", versionHandler)
    router.Run(":8080")
}

Initialize the module and fetch dependencies:

go mod init test
go env -w GOPROXY=https://goproxy.cn,direct
go mod tidy

Compile the binary for Linux:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.go

Create a Dockerfile that uses a lightweight Alpine base, copies the binary, and runs it:

FROM alpine
WORKDIR /data/app
ADD k8s-demo /data/app/
CMD ["/bin/sh","-c","./k8s-demo"]

Build and run the image:

docker build -t xianchao/k8sdemo:v1 .
docker run -d --name go -p 30180:8080 xianchao/k8sdemo:v1

Verify the service from a machine on the same network: curl http://<em>HOST_IP</em>:30180/ Expected JSON output: {"status":"success~welcome to study"} Check the version endpoint:

curl http://<em>HOST_IP</em>:30180/version
{"version":"v1.1版本"}

Conclusion

The two examples demonstrate end‑to‑end Docker image creation: one using a Rocky Linux base to serve a custom Nginx page, and another compiling a Go Gin web service into a minimal Alpine container, covering all necessary commands for building, running, and testing the containers.

DockerGocontainerDockerfileimageGin
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.