使用dockerfile发布go项目

docker安装

下载docker
yum install docker
 
设置docker随系统启动
chkconfig docker on
 
启动docker服务
service docker start
 
启动后查看docker状态
systemctl status docker:
[root@dev-k8s ~]# systemctl status docker● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-06-13 15:50:16 CST; 7min ago
     Docs: http://docs.docker.com
 Main PID: 12197 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─12197 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec...
           └─12207 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --s...
 
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.283640021+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.323651305+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.324181137+08:00" level=info msg="Loading containers: start."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.327645142+08:00" level=info msg="Firewalld running: false"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.421269250+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 192.168.0.0/24. Daemon optio...d IP address"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.452900303+08:00" level=info msg="Loading containers: done."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572134045+08:00" level=info msg="Daemon has completed initialization"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572172608+08:00" level=info msg="Docker daemon" commit="7d71120/1.13.1" graphdriver=overlay2 version=1.13.1
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.585165447+08:00" level=info msg="API listen on /var/run/docker.sock"
Jun 13 15:50:16 dev-k8s systemd[1]: Started Docker Application Container Engine.

go-helloworld代码

package main
 
import (
   "fmt"
   "log"
   "net/http"
   "os"
)
 
func handler(w http.ResponseWriter, r *http.Request) {
   log.Print("Hello world received a request.")
   target := "Welcome to go"
   fmt.Fprintf(w, "Hello, %s!\n", target)
}
 
func main() {
   log.Print("Hello world sample started.")
 
   http.HandleFunc("/", handler)
 
   port := os.Getenv("PORT")
   if port == "" {
      port = "8009"
   }
 
   log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
 
}

go项目打包

CMD中进入main方法目录设置linux运行环境 后 打包
F:\私人代码\go-helloword>set GOARCH=amd64
F:\私人代码\go-helloword>set GOOS=linux
 
 
环境问题导致打包出错,使用go env -w GO111MODULE=off命令解决下
F:\私人代码\go-helloword>go build -o helloworld
go: cannot find main module, but found .git/config in F:\私人代码\go-helloword
to create a module there, run:
go mod init
F:\私人代码\go-helloword>go mod init
go: cannot determine module path for source directory F:\私人代码\go-helloword (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
F:\私人代码\go-helloword>go env -w GO111MODULE=off
 
 
重新打包后成功
F:\私人代码\go-helloword>go build -o helloworld
F:\私人代码\go-helloword>dir
驱动器 F 中的卷是 文档及代码
卷的序列号是 DB44-E624
F:\私人代码\go-helloword 的目录
2022/06/13 16:37 <DIR> .
2022/06/13 16:37 <DIR> ..
2022/06/10 16:43 6,148 .DS_Store
2022/06/13 10:33 <DIR> .idea
2022/06/13 16:20 425 Dockerfile
2022/06/13 16:37 6,066,257 helloworld
2022/06/10 17:50 448 helloworld.go
4 个文件 6,073,278 字节
3 个目录 307,233,075,200 可用字节

dockerfile编写,需要加上RUN chmod +x /build/helloworld命令,否则导致容器启动时对go二进制文件helloworld没有使用权限

FROM golang:alpine
 
WORKDIR /build
 
COPY helloworld .
 
EXPOSE 8009
 
RUN chmod +x /build/helloworld
 
CMD ["/build/helloworld"]

使用dockerfile创建镜像,然后使用镜像创建容器

使用dockerfile创建镜像
[root@dev-k8s docker-go]# docker build . -t go-helloworld:0.0.4
Sending build context to Docker daemon 6.069 MB
Step 1/6 : FROM golang:alpine
 ---> 155ead2e66ca
Step 2/6 : WORKDIR /build
 ---> Using cache
 ---> 6b6f9f40297e
Step 3/6 : COPY helloworld .
 ---> 44fefa2c08df
Removing intermediate container f2ed9e5c80b6
Step 4/6 : EXPOSE 8009
 ---> Running in 3dff69240776
 ---> b7ce1ff0c201
Removing intermediate container 3dff69240776
Step 5/6 : RUN chmod +x /build/helloworld
 ---> Running in 344e235c6ee8
 
 ---> 2dea00206ded
Removing intermediate container 344e235c6ee8
Step 6/6 : CMD /build/helloworld
 ---> Running in a8a56639f2fb
 ---> df2ca4071b8c
Removing intermediate container a8a56639f2fb
Successfully built df2ca4071b8c
 
 
用镜像创建容器
root@dev-k8s ~]# docker run -d -it -p 8009:8009  go-helloworld:0.0.4
29ea4e02b8319f564b6cf7073792f275c754fcbeb08bb6062683c1524f9cbc7c

访问helloworld项目

root@dev-k8s docker-go]# netstat -antp |grep 8009
tcp        0      0 10.6.0.11:33522         172.22.254.3:8009       ESTABLISHED 17128/n9e-agent
tcp6       0      0 :::8009                 :::*                    LISTEN      12318/docker-proxy-
 
[root@dev-k8s docker-go]# curl localhost:8009
Hello, Welcome to go!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 使用Dockerfile制作web项目镜像的步骤如下: 1. 创建一个名为Dockerfile的文件,该文件可以位于上下文目录中,也可以位于其他目录中。\[2\] 2. 在Dockerfile中编写构建镜像的指令,例如安装依赖、复制文件等。可以根据需要自定义指令来构建镜像。 3. 使用build命令构建镜像,可以使用-t参数指定镜像的名称和标签。如果Dockerfile文件位于上下文目录中,并且文件名为Dockerfile,则可以直接使用-t参数指定镜像名和标签,例如:docker build -t mytomcat .\[1\] 4. 如果Dockerfile文件不位于上下文目录中,或者文件名不是Dockerfile,则可以使用-f参数指定Dockerfile的路径,例如:docker build -t mytomcat -f ../Dockerfile.php .\[2\] 5. 等待镜像构建完成,即可使用该镜像来运行web项目。 总结起来,使用Dockerfile制作web项目镜像的关键是编写Dockerfile文件,并使用build命令来构建镜像。可以根据需要自定义Dockerfile中的指令来满足项目的需求。 #### 引用[.reference_title] - *1* [使用DockerFile 制作镜像以及相关的命令](https://blog.csdn.net/qq_39445165/article/details/124636515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [使用Dockerfile制作镜像](https://blog.csdn.net/flyshuaibi/article/details/126468414)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值