Skip to content

Instantly share code, notes, and snippets.

@acondrat
Last active March 13, 2020 16:20
Show Gist options
  • Save acondrat/d9f66b9adf1d65b904d9f91eb14aad7e to your computer and use it in GitHub Desktop.
Save acondrat/d9f66b9adf1d65b904d9f91eb14aad7e to your computer and use it in GitHub Desktop.
cgroup in go
FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git
COPY hello.go hello.go
RUN go get -d -v
RUN go build -o /go/bin/hello
FROM scratch
COPY --from=builder /go/bin/hello /go/bin/hello
ENTRYPOINT ["/go/bin/hello"]
package main
import (
"fmt"
"io/ioutil"
"log"
_ "go.uber.org/automaxprocs"
)
func main() {
files, err := ioutil.ReadDir("/sys/fs/cgroup")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
fmt.Println(f.Name())
}
}
@acondrat
Copy link
Author

acondrat commented Mar 13, 2020

Seems to work as expected

$ docker run -it --rm --cpus=2 bbd1f832167e 
2020/03/13 16:18:42 maxprocs: Updating GOMAXPROCS=2: determined from CPU quota
blkio
cpu
cpu,cpuacct
cpuacct
cpuset
devices
freezer
hugetlb
memory
net_cls
net_cls,net_prio
net_prio
perf_event
pids
rdma
systemd
$ docker run -it --rm --cpus=3 bbd1f832167e 
2020/03/13 16:18:51 maxprocs: Updating GOMAXPROCS=3: determined from CPU quota
blkio
cpu
cpu,cpuacct
cpuacct
cpuset
devices
freezer
hugetlb
memory
net_cls
net_cls,net_prio
net_prio
perf_event
pids
rdma
systemd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment