➜ docker-compose build WARNING: Some networks were defined but are not used by any service: backend Building app Step 1/11 : FROM golang:1.16.9-stretch AS builder ---> 8a22bd5879d4 Step 2/11 : ENV GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY="https://goproxy.cn,direct" ---> Using cache ---> f1201d2611b7 Step 3/11 : WORKDIR /build ---> Using cache ---> 7e157372b7c9 Step 4/11 : COPY . . ---> 515c90ffcd8d Step 5/11 : RUN go mod download ---> Running in 0a52cfdd5444 Removing intermediate container 0a52cfdd5444 ---> 6f5f983a8fd5 Step 6/11 : RUN go build -o app . ---> Running in a76302af8f8e Removing intermediate container a76302af8f8e ---> 7195b4de462c Step 7/11 : FROM registry.cn-beijing.aliyuncs.com/liuqinghui/go-env:stretch-slim ---> f9204dd33156 Step 8/11 : WORKDIR /www ---> Using cache ---> b87192a29265 Step 9/11 : ARG APP_ENV ---> Using cache ---> fad4cc8a171b Step 10/11 : COPY ./config-${APP_ENV}.yaml /www/config.yaml ---> Using cache ---> bc7e2bb9dfcb Step 11/11 : COPY --from=builder /build/app /www ---> 951dce0d6536 Successfully built 951dce0d6536 Successfully tagged fund-server_app:latest
6.运行docker-compose up -d
# 运行 ➜ docker-compose up -d WARNING: Some networks were defined but are not used by any service: backend fund-server_app_1 is up-to-date
# 查看日志 ➜ docker-compose logs WARNING: Some networks were defined but are not used by any service: backend Attaching to fund-server_app_1 app_1 | [GIN-debug] [WARNING] Running in"debug" mode. Switch to "release" mode in production. app_1 | - using env: export GIN_MODE=release app_1 | - using code: gin.SetMode(gin.ReleaseMode) app_1 | app_1 | [GIN-debug] GET /system/config --> 52lu/fund-analye-system/api/demo.GetConfig (3 handlers) app_1 | [GIN-debug] POST /demo/user/login --> 52lu/fund-analye-system/api/demo.Login (3 handlers) app_1 | 【 当前环境: dev 当前版本: v1.0.0 接口地址: http://0.0.0.0:8081 启动时间:2021-10-18 22:10:23 】
7. 踩坑记录connect: no route to host
启动容器后,在容器内访问本地mysql报错: connect: no route to host
7.1 报错信息
➜ docker-compose up WARNING: Some networks were defined but are not used by any service: backend Starting fund-server_app_1 ... done Attaching to fund-server_app_1 app_1 | panic: 创建mysql客户端失败: dial tcp 172.20.0.1:3306: connect: no route to host, {172.20.0.1 3306 root root fund_system utf8mb4 falsetrue Local 255 truefalse {falsetruefalsefalse fas_ true} 50ms info truetrue} app_1 | app_1 | goroutine 1 [running]: app_1 | 52lu/fund-analye-system/initialize.initGorm() app_1 | /build/initialize/gorm.go:50 +0x6ae app_1 | 52lu/fund-analye-system/initialize.SetLoadInit() app_1 | /build/initialize/load.go:9 +0x2f app_1 | main.main() app_1 | /build/main.go:18 +0x45 fund-server_app_1 exited with code 2
7.2 解决方法
1. 替换mysql的host
# 查看网络信息 ➜ docker network ls NETWORK ID NAME DRIVER SCOPE 1ff63fe03b51 bridge bridge local 4067e988f21e fund-server_default bridge local 3e3c8036f789 host host local 5066fef7de07 none null local
# 文件位置 <?xml version="1.0" encoding="utf-8"?> <zone> <short>Public</short> <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description> <service name="ssh"/> <service name="dhcpv6-client"/> <port protocol="tcp" port="20"/> <port protocol="tcp" port="21"/> <port protocol="tcp" port="22"/> <port protocol="tcp" port="80"/> <port protocol="tcp" port="8888"/> <port protocol="tcp" port="39000-40000"/> <port protocol="tcp" port="443"/> <port protocol="tcp" port="12345"/> <port protocol="udp" port="12345"/> <port protocol="tcp" port="888"/>