DockerFile部署goweb实例

6 篇文章 0 订阅

一、本机环境

Centos7.6

二、项目结构

message-master/
├── common
│   ├── captcha.go
│   ├── db.go
│   ├── helper.go
│   ├── mysql.go
│   ├── parse.go
│   └── response.go
├── dockerfile
├── go.mod
├── go.sum
├── main.go
├── models
│   ├── article.go
│   ├── category.go
│   ├── comments.go
│   ├── friends.go
│   ├── gousers.go
│   └── stat.go
├── router
│   ├── article.go
│   ├── comment.go
│   ├── index.go
│   └── user.go
├── static
│   ├── layui
│   │   ├── css
│   │   ├── font
│   │   ├── images
│   │   ├── lay
│   │   ├── layui.all.js
│   │   └── layui.js
│   └── style.css
├── templates
│   ├── base.html
│   ├── detail.html
│   ├── friends.html
│   ├── index.html
│   ├── login.html
│   ├── message.html
│   ├── password.html
│   ├── profile.html
│   ├── publish.html
│   ├── register.html
│   └── space.html
└── upload
    ├── 202253
    │   ├── 1.jpg
    │   ├── BMW.jpg
    │   └── bugati.jpg
    └── 202255
        └── psb22GLGEDP.jpg

三、实现步骤

1、拉取MySQL镜像

[root@jacson ~]#docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

2、启动MySQL镜像

#外部端口13306映射内部3306端口
[root@jacson ~]#docker run --name mysql-web -p 13306:3306 -e MYSQL_ROOT_PASSWORD=root -v /Users/docker/mysql:/var/lib/mysql -d mysql:5.7
d7fbefd4845f9e8b1a0768a0459d1f2128b302d0923030cc78a6fd8128a4f344

3、本机Navicat连接启动的MySQL

ip:Linux主机ip
端口:外部映射端口
在这里插入图片描述通过sql文件导入数据库数据
在这里插入图片描述

4、编写DockerFile文件

FROM golang:alpine AS builder

# 为我们的镜像设置必要的环境变量、一定要配置GOPROXY,不然很多包下载不下来
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    GOPROXY=https://goproxy.cn,direct

# 移动到工作目录:/build
WORKDIR /build

# 复制项目中的 go.mod 和 go.sum文件并下载依赖信息
COPY go.mod .
COPY go.sum .
RUN go mod download

# 将代码复制到容器中
COPY . .

# 将我们的代码编译成二进制可执行文件 bubble
RUN go build -o message .

###################
# 接下来创建一个小镜像
###################
FROM scratch

COPY ./templates /templates
COPY ./static /static
COPY ./upload /upload

# 从builder镜像中把/dist/app 拷贝到当前目录
COPY --from=builder /build/message /

# 需要运行的命令
ENTRYPOINT ["/message"]

5、修改程序的数据库连接信息

#HOST是指向容器名
USER_NAME="root"
PASS_WORD="root"
HOST="mysql-web"
PORT="3306"
DATABASE="testdb"
CHARSET="utf8"

6、构建message_web镜像

[root@jacson ~/message-master]#docker build -t message_web .
Sending build context to Docker daemon  5.534MB
Step 1/14 : FROM golang:alpine AS builder
 ---> d8bf44a3f6b4
Step 2/14 : ENV GO111MODULE=on     CGO_ENABLED=0     GOOS=linux     GOARCH=amd64     GOPROXY=https://goproxy.cn,direct
 ---> Running in 72e9cd6878d1
Removing intermediate container 72e9cd6878d1
 ---> e0a0c809c2e3
Step 3/14 : WORKDIR /build
 ---> Running in 05725bcd004e
Removing intermediate container 05725bcd004e
 ---> 11a0af36638b
Step 4/14 : COPY go.mod .
 ---> 6de67c7bc94f
Step 5/14 : COPY go.sum .
 ---> 55d799bbc839
Step 6/14 : RUN go mod download
 ---> Running in c8591c710ad3
Removing intermediate container c8591c710ad3
 ---> 3d56f5da236e
Step 7/14 : COPY . .
 ---> 2d4e1d346834
Step 8/14 : RUN go build -o message .
 ---> Running in b080c442a8a7
Removing intermediate container b080c442a8a7
 ---> b9e3f102087e
Step 9/14 : FROM scratch
 ---> 
Step 10/14 : COPY ./templates /templates
 ---> 827adbd52265
Step 11/14 : COPY ./static /static
 ---> c72b91a0e9e5
Step 12/14 : COPY ./upload /upload
 ---> 10f55808c7b8
Step 13/14 : COPY --from=builder /build/message /
 ---> 4250300e9753
Step 14/14 : ENTRYPOINT ["/message"]
 ---> Running in 1a69d9f379e8
Removing intermediate container 1a69d9f379e8
 ---> a3f7b99f047a
Successfully built a3f7b99f047a
Successfully tagged message_web:latest

7、启动容器

运行message_web容器时,需要使用–link的方式与上面的mysql-web容器关联起来,具体命令如下:

[root@jacson ~/message-master]#docker run --link=mysql-web:mysql-web -p 9099:9099 message_web
[root@jacson ~/message-master]#docker run --link=mysql-web:mysql-web -p 9099:9099 message_web
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] GET    /upload/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /upload/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] Loaded HTML Templates (16): 
	- detail.html
	- publish.html
	- space.html
	- base.html
	- header
	- head
	- profile.html
	- 
	- footer
	- user-nav-left
	- message.html
	- password.html
	- friends.html
	- index.html
	- login.html
	- register.html

[GIN-debug] GET    /                         --> message/router.Index (4 handlers)

在这里插入图片描述
参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值