所以如果要用來 run 自己的 program,一定要是 static linked。
最好的說明就是docker 的hello-world image。
clone 下來以後,到 amd64/hello-world 看,就是做出 hello-world image 的 程式(hello) 和 Dockerfile。
用 file 來看 hello ,就是 statically linked, executable program
Dockerfile 就很簡單:
FROM scratch COPY hello / CMD ["/hello"]然後 build image:
docker build -t myhello . --no-cacherun image:
docker run myhello Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/這個 image 很小,就跟 hello 一樣大:
docker images EPOSITORY TAG IMAGE ID CREATED SIZE myhello latest 03cc4ebe3142 48 seconds ago 13.3kB ls -l hello -rwxrwxr-x 1 charles-chang charles-chang 13256 Sep 27 15:55 hello
另一個,簡單,用 go 做 example。一樣的 hello :building minimal docker image for go application
main.go:
package main import ( "fmt" "time" ) func main() { for { fmt.Println("Hello World!") time.Sleep(1 * time.Second) } }build for static:
$CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main然後Dockerfile 一樣:
FROM scratch ADD main / CMD ["/main"]然後就跟上面一樣,build image, run ....
以前好像寫過,go cross build 好方便,上面那個 GOARCH=arm64 就是 build for aarch64
沒有留言:
張貼留言