Skip to content

Instantly share code, notes, and snippets.

@tphummel
Last active July 22, 2020 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tphummel/53cbb25f67d7b38497b77efc66f02e46 to your computer and use it in GitHub Desktop.
Save tphummel/53cbb25f67d7b38497b77efc66f02e46 to your computer and use it in GitHub Desktop.
Http server binary in a scratch container

golang http server in a scratch container

docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:21:11 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

go version
go version go1.14.6 darwin/amd64

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build http.go

docker image build -t http .
docker container run -p 8090:8090 http

curl -v localhost:8090/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8090 (#0)
> GET / HTTP/1.1
> Host: localhost:8090
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 22 Jul 2020 04:40:06 GMT
< Content-Length: 6
< Content-Type: text/plain; charset=utf-8
< 
hello
* Connection #0 to host localhost left intact
* Closing connection 0
FROM scratch
COPY ./http /bin/app
ENTRYPOINT ["/bin/app"]
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello\n")
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func main() {
http.HandleFunc("/", hello)
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
http.ListenAndServe(":8090", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment