背景:
我们需要将本地开发的golang项目上线测试环境,需要将golang项目打包成docker image 在服务器上运行
解决办法:
1.编写dockerfile
FROM golang:1.12.7
RUN go get github.com/astaxie/beego && go get github.com/beego/bee
RUN go get github.com/go-sql-driver/mysql
EXPOSE 8080
WORKDIR /go/src/WEB
CMD ["bee", "run"]
这个dockerfile 安装了三个依赖, 一个是beego框架本身,一个是bee工具 ,一个是 mysql的驱动
设置的工作环境是 /go/src/WEB ,之后 去工作区执行bee run 启动项目
2.执行构建镜像
docker build -f dockerfile -t beego:v2 .
$ docker build -f dockerfile -t beego:v2 .
Sending build context to Docker daemon 32.74MB
Step 1/6 : FROM golang:1.12.7
---> be63d15101cb
Step 2/6 : RUN go get github.com/astaxie/beego && go get github.com/beego/bee
---> Using cache
---> 61b9a9d7d0ee
Step 3/6 : RUN go get github.com/go-sql-driver/mysql
---> Running in e076e662efcc
Removing intermediate container e076e662efcc
---> 615393b43fa8
Step 4/6 : EXPOSE 8080
---> Running in bb5d89a4843d
Removing intermediate container bb5d89a4843d
---> b29220bb61f1
Step 5/6 : WORKDIR /go/src/WEB
---> Running in c74b08531265
Removing intermediate container c74b08531265
---> a1c6a30a2ea7
Step 6/6 : CMD ["bee", "run"]
---> Running in 09adba2452a2
Removing intermediate container 09adba2452a2
---> 6c10d5f5d414
Successfully built 6c10d5f5d414
Successfully tagged beego:v2
查看构建出来的image
$ docker images | grep beego
beego v2 6c10d5f5d414 2 hours ago 873MB
beego latest 42e9ce6cabc3 2 hours ago 870MB
3.运行容器
->docker run -itd --name cfss -p 8082:8080 -v $GOPATH/src/WEB:/go/src/WEB beego:v2
e2abc7720a54c3002b137af4b96342aa7e2d63b0c4b1c20cec7e08eda439419e
->docker logs -f cfss
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.10.0
2019/11/18 05:30:55 INFO ▶ 0001 Using 'WEB' as 'appname'
2019/11/18 05:30:55 INFO ▶ 0002 Initializing watcher...
github.com/astaxie/beego/orm
github.com/astaxie/beego/config
github.com/astaxie/beego/utils
github.com/astaxie/beego/session
github.com/astaxie/beego/vendor/gopkg.in/yaml.v2
github.com/astaxie/beego/context
github.com/shiena/ansicolor
github.com/astaxie/beego/logs
WEB/models
github.com/astaxie/beego/grace
github.com/astaxie/beego/context/param
github.com/astaxie/beego/toolbox
github.com/astaxie/beego/vendor/golang.org/x/crypto/acme
github.com/astaxie/beego/httplib
sgithub.com/astaxie/beego/vendor/golang.org/x/crypto/acme/autocert
WEB/controllers
WEB/routers
WEB
2019/11/18 05:31:07 SUCCESS ▶ 0003 Built Successfully!
2019/11/18 05:31:07 INFO ▶ 0004 Restarting 'WEB'...
2019/11/18 05:31:07 SUCCESS ▶ 0005 './WEB' is running...
^[OB^[OB^[OB^[OB^[OB^[OB^[OB^[OB^[OB2019/11/18 05:31:07.708 [I] [asm_amd64.s:1337] http server Running on http://:8080