Source codde 請參考  https://github.com/chio-nzgft/Docker-golang-web

先寫個 Dockerfile ....上網找個 OS 比較完整也比較小的 ...找到 opensuse ....

FROM opensuse:latest
COPY web /web
ENTRYPOINT /web
EXPOSE 9090


再寫個 web.go  ...例如 9090 port

[root@test web]# more web.go

package main

import (
        "fmt"
        "net/http"
        "github.com/ajstarks/svgo"
        "math/rand"
        "time"
        "log"
)

const arcfmt = "stroke:%s;stroke-opacity:%.2f;stroke-width:%dpx;fill:none"

var colors = []string{"red", "green", "blue", "white", "gray"}

func randarc(canvas *svg.SVG, aw, ah, sw int, f1, f2 bool) {
        begin := rand.Intn(aw)
        arclength := rand.Intn(aw)
        end := begin + arclength
        baseline := ah / 2
        al := arclength / 2
        cl := len(colors)
        canvas.Arc(begin, baseline, al, al, 0, f1, f2, end, baseline,
                fmt.Sprintf(arcfmt, colors[rand.Intn(cl)], rand.Float64(), rand.Intn(sw)))

}

func main() {
        http.Handle("/", http.HandlerFunc(circle))
        err := http.ListenAndServe(":9090", nil)
        if err != nil {
                log.Fatal("ListenAndServe:", err)
        }
}

func circle(w http.ResponseWriter, req *http.Request) {
        w.Header().Set("Content-Type", "image/svg+xml")

        rand.Seed(time.Now().UnixNano())

        width := 800
        height := 800
        aw := width / 2
        maxstroke := height / 10
        literactions := 50
        canvas := svg.New(w)
        canvas.Start(width, height)
        canvas.Rect(0, 0, width, height)
        for i := 0; i < literactions; i++ {
                randarc(canvas, aw, height, maxstroke, false, true)
                randarc(canvas, aw, height, maxstroke, false, false)
        }
        canvas.End()
}

 

建立 web 執行檔 

[root@test web]# go get github.com/ajstarks/svgo
[root@test web]# go build web.go

建立 docker image web

[root@test web]# docker build -t web .
Sending build context to Docker daemon 33.03 MB
Step 1 : FROM opensuse:latest
Trying to pull repository docker.io/library/opensuse ...
latest: Pulling from docker.io/library/opensuse
09c979f45514: Pull complete
Digest: sha256:894a8214e62bfd0d0eacda1734180add36c521d352d7e60a91d5ae07cbb15c09
Status: Downloaded newer image for docker.io/opensuse:latest
 ---> 09b54e62aa03
Step 2 : COPY web /web
 ---> 3df4f2604b12
Removing intermediate container 57be8986c291
Step 3 : ENTRYPOINT /web
 ---> Running in 9c826beac1d0
 ---> 362d33afc382
Removing intermediate container 9c826beac1d0
Step 4 : EXPOSE 9090
 ---> Running in 992e64ef5e90
 ---> 23accfd5cd07
Removing intermediate container 992e64ef5e90
Successfully built 23accfd5cd07

執行 docker  image 到  container  

[root@test web]# docker run -d -p 192.168.0.70:9093:9090 web
fa80bc1326fc48643dfcea13200acc27631b39444354f605f0cd17b8d0784f51
[root@test web]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                         NAMES
fa80bc1326fc        web                 "/bin/sh -c /web"   7 seconds ago       Up 4 seconds        192.168.0.70:9093->9090/tcp   hungry_mestorf

 

查看 images 大小: 大約 141M

[root@test web]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
web                  latest              23accfd5cd07        5 minutes ago       140.5 MB
docker.io/opensuse   latest              09b54e62aa03        3 weeks ago         132.4 MB
 

開 web

文章標籤
全站熱搜
創作者介紹
創作者 echochio 的頭像
echochio

echochio

echochio 發表在 痞客邦 留言(0) 人氣(9)