docker运行一个二进制文件(部署go)
2019-04-28 本文已影响25人
黑客不黑_
1、项目目录结构
image.png
2、构建可执行二进制文件
#构建可执行二进制文件
CGO_ENABLED=0 GOARCH=amd64 go build -o bh-go-server-sms server.go
3、运行文件目录结构
go二进制文件bh-go-server-sms以及配置文件config.env
[root@bogon bh-go-server-sms]# tree
.
├── bh-go-server-sms
├── config
│?? ├── config.conf.go
│?? ├── config.env
│?? ├── config.env.example
│?? ├── readme_ssl.md
│?? ├── server.key
│?? └── server.pem
└── Dockerfile
1 directory, 8 files
4、通过Dockerfile创建镜像文件
[root@bogon bh-go-server-sms]# cat Dockerfile
FROM busybox
LABEL RoES roes@163.com
WORKDIR /root
ADD ./bh-go-server-sms ./bh-go-server-sms
ADD ./config ./config
EXPOSE 51003
ENTRYPOINT ["./bh-go-server-sms"]
# 基础镜像是docker仓库的busybox
FROM busybox
# 作者签名
MAINTAINER RoES roes@163.com
# WORKDIR 工作目录, 在docker中的目录,可以自己指定
# ADD [源文件] [目标文件]
# EXPOSE 80 端口号是多少
# ENTRYPOINT ["./bh-go-server-sms"] 这个是在运行容器时会执行的命令,运行二进制文件,容器运行go二进制文件跟随运行。
5、打包镜像
给go二进制文件授权执行权限
chmod 755 bh-go-server-sms
docker build -t go-server-sms .
.
表示使用当前上下文中的 Dockerfile 文件,创建镜像名为go-server-sms的镜像.
[root@bogon bh-go-server-sms]# docker build -t go-server-sms .
Sending build context to Docker daemon 18.77 MB
Step 1/7 : FROM busybox
---> af2f74c517aa
Step 2/7 : LABEL RoES roes@163.com
---> Using cache
---> f8a01dbd26dd
Step 3/7 : WORKDIR /root
---> Using cache
---> d039057082c0
Step 4/7 : ADD ./bh-go-server-sms ./bh-go-server-sms
---> Using cache
---> 13a21014f889
Step 5/7 : ADD ./config ./config
---> Using cache
---> 7e0cf19060c6
Step 6/7 : EXPOSE 51003
---> Using cache
---> 618490085100
Step 7/7 : ENTRYPOINT ./bh-go-server-sms
---> Using cache
---> 52f8514d592a
Successfully built 52f8514d592a
出现Successfully built则创建镜像成功,镜像ID为52f8514d592a
root@bogon bh-go-server-sms]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-server-sms latest 52f8514d592a 37 minutes ago 20 MB
6、启动镜像运行容器
docker run --name go-server-sms -d -p 51003:51003 bh-go-server-sms
7、查看
[root@bogon bh-go-server-sms]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e5d4f62c8dd bh-go-server-sms "./bh-go-server-sms" 37 minutes ago Up 37 minutes 0.0.0.0:51003->51003/tcp bh-go-server-sms
[root@bogon bh-go-server-sms]# ps -ef|grep ./bh-go-server-sms
root 3435 3418 0 15:49 ? 00:00:01 ./bh-go-server-sms
root 3539 1704 0 16:28 pts/0 00:00:00 grep --color=auto ./bh-go-server-sms
8、使用docker-compose部署
创建docker-compose.yml文件
[root@bogon bh-go-server-sms]# cat docker-compose.yml
version: "3"
services:
go-web:
image: bh-go-server-sms
ports:
- "51003:51003"
运行docker-compose
- docker-compose up -d 后台运行
[root@bogon bh-go-server-sms]# docker-compose up
Creating network "bh-go-server-sms_default" with the default driver
Creating bh-go-server-sms_go-web_1 ... done
Attaching to bh-go-server-sms_go-web_1
go-web_1 | time="2019-04-28T08:31:27Z" level=info msg="server addr : 172.18.0.2:51003"
go-web_1 | time="2019-04-28T08:31:27Z" level=info msg="[info] etcd key : /bh_server/sms/172.18.0.2:51003\n"
^CGracefully stopping... (press Ctrl+C again to force)
Killing bh-go-server-sms_go-web_1 ... done
[root@bogon bh-go-server-sms]# docker-compose up -d
Starting bh-go-server-sms_go-web_1 ... done