个人环境:
centos7.4
Docker version 19.03.13
学习课程 博客:带你十天轻松搞定 Go 微服务系列(一)
到第七章学习有小断层,你也可以直接第7章跑起来后 再写代码
一、创建目录 mkdir 与 touch 命令
cd 目录(你放项目的目录)
mkdir gonivinck
cd gonivinck
mkdir dtm
cd dtm
touch config.yml
touch Dockerfile
其他目录同上 返回上级目录 cd ../
二、编写 Dockerfile
不熟悉Dockerfile的点击这里
golang 容器的 Dockerfile
代码如下:
#基于golang:1.17 操作
FROM golang:1.17
.
.
.
已省略可直接copy原文
其他服务容器 Dockerfile
例子:/gonivinck/dtm/Dockerfile 其他的依此类推
FROM yedf/dtm
LABEL maintainer="Ving <ving@nivin.cn>"
三、编写 .env
配置文件
为了让环境运行 可以先直接复制博客上的 后续再手打下 加深印象
# 设置时区
TZ=Asia/Shanghai
# 设置网络模式
NETWORKS_DRIVER=bridge
.
.
.
已省略可直接copy原文
四、编写 docker-compose.yml 配置文件
不熟悉docker-composer.yml 点击这里
version: '3.5'
# 网络配置
networks:
backend:
driver: ${NETWORKS_DRIVER}
.
.
.
已省略可直接copy原文
五、构建运行
docker-compose up -d
docker-compose up -d
在后台启动环境启动容器 可能会忽略一些警告或bug
docker-compose up
会直观的在当前控制台启动 有bug 会显示
bug 1
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-dfbf6d743c13 -j RETURN: iptab les: No chain/target/match by that name.
(exit status 1))
解决:重启docker即可 service docker restart
bug 2
debconf: delaying package configuration, since apt-utils is not installed
解决:可以安装apt-utils,或忽略这个错误
bug 3 golang服务报错
package github.com/tal-tech/go-zero/tools/goctl@cli: cannot use path@version syntax in GOPATH mode
ERROR: Service 'golang' failed to build : The command '/bin/sh -c GOPROXY=https://goproxy.cn/,direct go install github.com/tal-tech/go-zero/tools/goctl@cli' returned a non-zero code: 1
解决:我的错误是因为 from go 1.17
被我改成 from go 1.15
网上类似情况解决
bug 4 signal: killed 云服务器配置(1核2g)较低
go build github.com/zeromicro/ddl-parser/gen: /usr/local/go/pkg/tool/linux_amd64/compile: signal: killed
ERROR: Service 'golang' failed to build : The command '/bin/sh -c GOPROXY=https://goproxy.cn/,direct go install github.com/tal-tech/go-zero/tools/goctl@cli' returned a non-zero code: 1
解决:
- 土豪:加钱直接升级云服务器配置
- 屌丝:配置 swap 分区 参考
bug 4 端口被占用
ERROR: for mysql Cannot start service mysql: driver failed programming external connectivity on endpoint gonivinck_mysql_1 (aac3792c0966c870d1b350c5e61589df38bdd14eb0ba567da463f05c94c3a22d): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use
ERROR: for redis Cannot start service redis: driver failed programming external connectivity on endpoint gonivinck_redis_1 (f09a8ca17958070e81d93b7cfff9e8826b18a3f4261a990c599a8e7366d8abbc): Error starting userland proxy: listen tcp 0.0.0.0:6379: bind: address already in use
ERROR: for golang Cannot start service golang: driver failed programming external connectivity on endpoint gonivinck_golang_1 (8430bb625604162ae64b5d2addadc9fb2b897cc8b6d87e3af5ff6ae6ca7496f2): Error starting userland proxy: listen tcp 0.0.0.0:9001: bind: address already in use
ERROR: Encountered errors while bringing up the project.
解决:
netstat -apn | grep 9001
netstat -apn | grep 3306
netstat -apn | grep 6379
找到进程 kill -9 pid 不行强行关掉宿主机占用端口的服务
docker-compose up -d
成功 我在我的云服务器启动了
gonivinck_etcd_1 is up-to-date
Starting gonivinck_golang_1 ...
gonivinck_prometheus_1 is up-to-date
gonivinck_grafana_1 is up-to-date
Starting gonivinck_mysql_1 ...
gonivinck_dtm_1 is up-to-date
gonivinck_jaeger_1 is up-to-date
Starting gonivinck_golang_1 ... done
Starting gonivinck_mysql_1 ... done
Creating gonivinck_mysql-manage_1 ... done
docker ps
六、 依次去访问 1000 2000 3000 4000 5000
bug 1
127.0.0.1:200 出现bug ERROR: Host is unreachable [tcp://redis:6379]
redis 未背解析?
docker-compose up 直接报错
Failed opening the RDB file crontab (in server root dir /etc) for saving: Permission denied
解决: 参考
2000端口折腾大半天 依旧无法访问 ,有搞定的大佬dddd me
20220215元宵节22点 回家刷新下 就好了 哈哈哈哈至此5个端口正常
bug 2
不小心exit 关闭了容器 进入 gonivick 目录 再次启动报错
(iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3306 -j DNAT --to-destination 172.19.0.2:3306 ! -i br-6f473ddbc488: iptables: No chain/target/match by that name.
可以采用下面这个命令 重启所有name 有gonivinck 字符的容器 参考
systemctl restart docker
docker start $(docker ps -a |grep gonivinck |awk '{ print $1}' | tail -n +2)
docker stop $(docker ps -a |grep gonivinck |awk '{ print $1}' | tail -n +2)
docker ps
//检查下是否还有未关闭的 有直接根据id 关闭 本人测试的关不全