Dockerizing Your Go Application

Prerequisites

Jumping In

So first we’ll create our Dockerfile ready to run our compiled web server, called main

FROM scratchADD main /EXPOSE 80
CMD ["/main"]
version: "2"services:
application:
container_name: application
build: .
ports:
- 80:80
environment:
- HOST=:80
build:
./build.sh
docker-build: build
docker-compose up --build
docker-up:
docker-compose up
docker-rm:
docker rm application
default: build
# Remove the existing binary
rm main
# Build a Go binary for our linux scratch image
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Build the docker image
docker build .
import "github.com/gin-gonic/gin"
import "os"
import "net/http"
func main() {
router := gin.Default()

router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Hello": "World",
})
})
router.Run(os.Getenv("HOST"))
}
{
"Hello": "World"
}

--

--

Multi-pronged web developer with a passion for cutting edge tooling! https://craigchilds.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store