其他分享
首页 > 其他分享> > Dockerfile部署golang项目

Dockerfile部署golang项目

作者:互联网

使用docker运行一个简单的httpserver
项目源码没几行,很简单,golang1.14

Dockerfile

FROM golang:1.14 AS build
COPY . /opt/CloudNative
WORKDIR /opt/CloudNative
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o httpserver

FROM alpine
COPY --from=build /opt/CloudNative/httpserver /opt/CloudNative/httpserver
EXPOSE 8080
ENTRYPOINT ["/opt/CloudNative/httpserver"]

构建镜像脚本

#!/usr/bin/env sh
version=$(date +%Y%m%d.%H%M%S)
img=sjjwantfish/cloudnative:v$version
echo "\033[32m""$img""\033[0m"
docker build . -t $img
docker push $img

构建&运行

# 在Dockerfile目录下运行build.sh
sh build.sh  # 输出忽略
docker run -d -p 8080:8080 sjjwantfish/cloudnative:v20211019.105456
49baa17c4018c8a33e5c58b4d94a929a0c2494af1abe946cb0e74b69ee018113

docker ps
CONTAINER ID   IMAGE                                      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
49baa17c4018   sjjwantfish/cloudnative:v20211019.105456   "/opt/CloudNative/ht…"   3 seconds ago   Up 2 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   compassionate_brown

# 访问容器服务
curl -s 'http://127.0.0.1:8080/healthz'
{"request":"/healthz"}%

docker inspect compassionate_brown | grep -i pid
            "Pid": 4079,
            "PidMode": "",
            "PidsLimit": null,
# 查看项目ns的ip
nsenter -t 4079 -n ip addr

标签:opt,httpserver,8080,部署,CloudNative,golang,build,docker,Dockerfile
来源: https://blog.csdn.net/weixin_43515220/article/details/120843903