golang项目编译生成docker镜像发布部署

2021-11-25  本文已影响0人  王宣成

windows下编译成Linux下可执行的二进制文件main

SET CGO_ENABLED=0
SET GOOS=linux
# SET GOOS=windows
SET GOARCH=amd64
go build -o main main.go

linux 下编译成可执行的二进制文件main

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go

Dockerfile文件参考

FROM loads/alpine:3.8

# 设置固定的项目路径
ENV WORKDIR /var/www/main

# 添加应用可执行文件,并设置执行权限
ADD ./main   $WORKDIR/main
RUN chmod +x $WORKDIR/main

# 添加静态文件、配置文件、模板文件
ADD public   $WORKDIR/public
ADD config   $WORKDIR/config
ADD template $WORKDIR/template

WORKDIR $WORKDIR

ENTRYPOINT ["./main"]

指令编译生成名为main的docker镜像

docker build -t main .

运行镜像

docker run main

登录阿里云Docker Registry , 用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码

docker login --username=176817****@qq.com registry.cn-hangzhou.aliyuncs.com
image.png

将镜像推送到Registry

# docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/[命名空间]/[仓库名]:[镜像版本号]
docker tag f42d282f29c7 registry.cn-hangzhou.aliyuncs.com/wangxuancheng/gf:v1
docker push registry.cn-hangzhou.aliyuncs.com/wangxuancheng/gf:v1

拉取镜像

docker pull registry.cn-hangzhou.aliyuncs.com/wangxuancheng/gf:v1

运行镜像

docker run --name gf \
-p 8199:8199 \
-v /www/wwwroot/gf/docker/uploads:/var/www/main/uploads \
-v /www/wwwroot/gf/docker/config:/var/www/main/config \
-v /www/wwwroot/gf/docker/tmp:/var/www/main/tmp \
-d registry.cn-hangzhou.aliyuncs.com/wangxuancheng/gf:v1

nginx代理参考

server {
    listen       80;
    server_name  goframe.org;

    access_log   /var/log/gf-app-access.log;
    error_log    /var/log/gf-app-error.log;

    location ^~ /public {
        access_log off;
        expires    1d;
        root       /var/www/gf-app;
        try_files  $uri @backend;
    }

    location / {
        try_files $uri @backend;
    }

    location @backend {
        proxy_pass                 http://127.0.0.1:8199;
        proxy_redirect             off;
        proxy_set_header           Host             $host;
        proxy_set_header           X-Real-IP        $remote_addr;
        proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

上一篇下一篇

猜你喜欢

热点阅读