ubuntu@shenzhen:~ $ cat hello.go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
ubuntu@shenzhen:~ $ go build hello.go
ubuntu@shenzhen:~ $ ./hello
hello world
ubuntu@shenzhen:~ $ mkdir gohello
ubuntu@shenzhen:~ $ cp hello gohello/
ubuntu@shenzhen:~ $ cd gohello/
ubuntu@shenzhen:~/gohello $ ls
hello
ubuntu@shenzhen:~/gohello $ vim Dockerfile
ubuntu@shenzhen:~/gohello $ docker build -t myhello:latest .
Sending build context to Docker daemon 2.012MB
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY hello /
---> ca4b658b1549
Step 3/3 : CMD ["/hello"]
---> Running in 2ebeeef54814
Removing intermediate container 2ebeeef54814
---> bd91ff0fe143
Successfully built bd91ff0fe143
Successfully tagged myhello:latest
ubuntu@shenzhen:~/gohello $ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myhello latest bd91ff0fe143 4 seconds ago 2.01MB
ubuntu@shenzhen:~/gohello $ docker container run myhello
hello world
Docker自制golang版helloworld
最新推荐文章于 2025-10-14 19:02:45 发布
本文详细展示了如何使用Go语言编写一个简单的程序并构建Docker镜像的过程。从源代码编写、编译到Dockerfile的创建,最后通过Docker命令完成镜像构建和运行,全程实战操作。
1019

被折叠的 条评论
为什么被折叠?



