#安装docker#!/bin/sh#关闭SeLinuxsed -i 's/enforcing/disabled/g' /etc/sysconfig/selinuxsetenforce 0#关闭防火墙systemctl stop firewalldsystemctl disable firewalld#安装docker1,curl -fsSL https://get.docker.com/ | sh2,yum install -y yum-utilsyum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repoyum install docker-ce docker-ce-cli containerd.io#添加阿里DNStouch /etc/docker/daemon.jsonvi /etc/docker/daemon.json{"dns": ["223.5.5.5","223.6.6.6"]}#开启docker服务并开启开机自启动systemctl start dockersystemctl enable dockercentos8 TencentOShttps://blog.csdn.net/qq_31803267/article/details/122960656#创建名为mynet的网络docker network create mynet#安装docker-compose#(直接下载安装)curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-composeln -s /usr/local/bin/docker-compose /usr/bin/docker-compose#(pip安装)1、安装python-pipyum -y inst
直接运行
#设置基础镜像来源FROM golang:1.17-alpine#设置时区ENV TZ=Asia/ShanghaiRUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone#设置代理RUN go env -w GOPROXY=https://goproxy.cn,direct#设置工作目录WORKDIR /data/#复制代码COPY data/. .#安装环境组件RUN go mod tidy -compat=1.17#启动主程序#RUN go run main.go#设置端口EXPOSE 8080#设置启动命令CMD ["go","run","main.go"]
构建二进制运行
# 构建成二进制文件FROM golang:1.17-alpine# 开启 Go ModuleRUN go env -w GO111MODULE=onRUN go env -w GOPROXY=https://goproxy.cn,directWORKDIR /data/COPY . .RUN CGO_ENABLED=0 GOOS=linux go build -a -o app.api .# 配置运行环境FROM alpine:3# 设置为中国时区# 设置系统根证书RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositoriesRUN apk --no-cache add tzdata && \apk add ca-certificates && \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \echo "Asia/Shanghai" > /etc/timezoneENV TZ Asi