Skip to content

Instantly share code, notes, and snippets.

@pablotron
Created May 1, 2023 17:23
Show Gist options
  • Save pablotron/03785b66cc9e90ebd5fdaef5642baa6d to your computer and use it in GitHub Desktop.
Save pablotron/03785b66cc9e90ebd5fdaef5642baa6d to your computer and use it in GitHub Desktop.
podman test: go multistage build, rootless in bookworm
pabs@worm:~/golang-container-test> l
Dockerfile go.mod hi.go
pabs@worm:~/golang-container-test> cat go.mod
module hi
go 1.20
pabs@worm:~/golang-container-test> cat hi.go
package main
import (
"log"
"net/http"
"os"
)
func main() {
// get bind address
addr := ":4567"
if len(os.Args) > 1 {
addr = os.Args[1]
}
// bind handler to /
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hi!"))
})
// serve
log.Fatal(http.ListenAndServe(addr, nil))
}
pabs@worm:~/golang-container-test> cat Dockerfile
FROM docker.io/golang:1.20-alpine AS build
COPY . /src
WORKDIR /src
RUN ["go", "build", "-trimpath", "-ldflags=-s -w"]
FROM scratch
COPY --from=build /src/hi /hi
CMD ["/hi", ":4567"]
pabs@worm:~/golang-container-test> podman build -t go-hi .
[1/2] STEP 1/4: FROM docker.io/golang:1.20-alpine AS build
[1/2] STEP 2/4: COPY . /src
--> 9b83f0af9d9
[1/2] STEP 3/4: WORKDIR /src
--> 49f6a921ef8
[1/2] STEP 4/4: RUN ["go", "build", "-trimpath", "-ldflags=-s -w"]
--> f3fea1b1b2a
[2/2] STEP 1/3: FROM scratch
[2/2] STEP 2/3: COPY --from=build /src/hi /hi
--> Using cache a17890bd631213ff9185f270e44f5dffcdfb54edf0dc294afd1de6a7f57ce570
--> a17890bd631
[2/2] STEP 3/3: CMD ["/hi", ":4567"]
--> Using cache 8cccd4df594c57e609d12b95c8aae314efbd1da247d168c4c459ddeb5b469131
[2/2] COMMIT go-hi
--> 8cccd4df594
Successfully tagged localhost/go-hi:latest
8cccd4df594c57e609d12b95c8aae314efbd1da247d168c4c459ddeb5b469131
pabs@worm:~/golang-container-test> podman run --rm -d -p 4568:4567 go-hi
57729c23352307b46e2865b70d61d2d1d72bc36837ab250c1270ec5aeebce1b5
# test from a different machine
pabs@flex:~> curl http://192.168.122.156:4568/; echo
hi!
pabs@flex:~>
# check image sizes
pabs@worm:~/golang-container-test> podman images 'localhost/*'
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/go-hi latest 8cccd4df594c 8 minutes ago 4.55 MB
localhost/hi latest a88bf49da5c1 About an hour ago 921 MB
localhost/foobar latest 586d246d3e92 4 hours ago 14.2 MB
pabs@worm:~/golang-container-test>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment