Ref
Build your Go image
golang docker hub
Environment
- Use Docker Desktop (for Mac)
- Use
1.19.4-bullseye
not alpine - Not special go library dependency
Preparation
Create folder and file structure
goapp |- Dockerfile |- go.mod |- main.go
go.mod is by “go mod init command”
main.go
package main import ( "log" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World!")) }) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal(err) } }
Simple server code, only res when access “/”.
Port is 8080
Dockerfile
FROM golang:1.19.4-bullseye
WORKDIR /app
COPY go.mod ./
COPY *.go ./
RUN go build -o /go-app
EXPOSE 8080
CMD ["/go-app"]
Image is golan Debian bullseye.
Copy go mod and go files and build on the server. Build go files and start go app.
Build and Execute
Let’s build Dockerfile
docker build --tag first-go-app .
Set name “fitst-go-app”
Check build images
docker image ls
Can see
REPOSITORY TAG IMAGE ID CREATED SIZE
first-go-app latest 421e5e5fc8bb 55 seconds ago 850MB
850MB, … Debian is big.
Let’s run this
docker run --publish 8080:8080 first-go-app
コメント