doulangxun7769 2019-08-23 21:54
浏览 109

在Dockerfile和docker-compose中编码要传递本地软件包的内容

I introduced local package with go modules for server side api. It works fine in local environment without error by commanding go run main.go.

But it does not work when commanding docker-compose up.

I want to know how to fix Dockerfile and docker-compose.yml.

I command go mod init at article directory.

As result, it sets module github.com/jpskgc/article in go.mod.

article
  ├ db
  ├ client
  ├ api
  │  ├ main.go
  │  ├ controller
  │  │    └ controller.go
  │  └ Dockerfile
  ├ nginx
  ├ docker-compose.yml
  ├ go.mod
  └ go.sum

main.go

package main

import (
    "database/sql"
    "os"
    "time"
    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
    _ "github.com/go-sql-driver/mysql"
    "github.com/joho/godotenv"
    "github.com/jpskgc/article/api/controller"
)
    api := router.Group("/api")
    {
        api.GET("/articles", func(c *gin.Context) {
            controller.GetArticleController(c, db)
        })
    }
    router.Run(":2345")
}

Dockerfile

FROM golang:latest
ENV GO111MODULE=on
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
ENTRYPOINT ["/app/api"]

docker-compose.yml

version: '3'
services:
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./api
    volumes:
      - ./api:/app
    depends_on:
      - db
    tty: true
    environment:
      - AWS_ACCESS_KEY_ID
      - AWS_SECRET_ACCESS_KEY
      - MYSQL_USER
      - MYSQL_PASSWORD
      - MYSQL_HOST
      - GO111MODULE

go.mod

module github.com/jpskgc/article

go 1.12

require (
    github.com/aws/aws-sdk-go v1.23.7
    github.com/gin-contrib/cors v1.3.0
    github.com/gin-gonic/gin v1.4.0
    github.com/go-sql-driver/mysql v1.4.1
    github.com/google/uuid v1.1.1
    github.com/joho/godotenv v1.3.0
)

I expect server side api working fine without error in docker environment. But the actual is that docker environment for server side fails to run.

Here is error message when commanding docker-compose up

api_1     | main.go:14:2: cannot find package "github.com/jpskgc/article/api/controller" in any of:
api_1     |     /usr/local/go/src/github.com/jpskgc/article/api/controller (from $GOROOT)
api_1     |     /go/src/github.com/jpskgc/article/api/controller (from $GOPATH)

Here is the entire source code on github (branch: try-golang-mod).

  • 写回答

2条回答 默认 最新

  • douqin6785 2019-08-24 03:20
    关注

    This error message seems to indicate that Go modules are not enabled:

    Here is error message when commanding docker-compose up

    api_1     | main.go:14:2: cannot find package "github.com/jpskgc/article/api/controller" in any of:
    api_1     |     /usr/local/go/src/github.com/jpskgc/article/api/controller (from $GOROOT)
    api_1     |     /go/src/github.com/jpskgc/article/api/controller (from $GOPATH)
    

    I would double check that the GO111MODULE=on environment variable is being set and propagated properly.

    There are some other things you could check here:

    https://github.com/golang/go/wiki/Modules#what-are-some-general-things-i-can-spot-check-if-i-am-seeing-a-problem

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?