dqsvf28682 2019-08-25 16:20
浏览 200
已采纳

在Dockerfile和docker-compose.yml中编码的内容以在Docker环境中传递本地包

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 what to code Dockerfile and docker-compose.yml to fix.

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-gonic/gin"
    "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 /go/src/github.com/jpskgc/article/app
COPY . .
RUN go mod download
RUN go build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
ENTRYPOINT ["/go/src/github.com/jpskgc/article/app/api"]

docker-compose.yml

version: '3'
services:
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./api
    volumes:
      - ./api:/go/src/github.com/jpskgc/article/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

build command-line-arguments: 
cannot load github.com/jpskgc/article/api/controller: 
cannot find module providing package github.com/jpskgc/article/api/controller
article_api_1 exited with code 1

Here is the entire source code on github (branch: try-golang-mod). I wish someone to give me answer code for Dockerfile and docker-compose.yml. https://github.com/jpskgc/article/tree/try-golang-mod

  • 写回答

2条回答 默认 最新

  • dongshan9619 2019-08-27 15:49
    关注

    I resolve this issue in following way.

    1. go mod init article/api at api directory.
    article
      ├ client
      ├ api
      │  ├ main.go
      │  ├ contoroller
      │  │    └ contoroller.go
      │  ├ //
      │  ├ go.mod 
      │  ├ go.sum
      │  └ Dockerfile
      ├ nginx
      ├ go.mod
      ├ go.sum
      └ docker-compose.yml
    
    1. fix Docekrfile
    FROM golang:latest
    ENV GO111MODULE=on
    WORKDIR /app
    COPY go.mod .
    COPY go.sum .
    RUN go mod download
    COPY . .
    RUN go build
    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
    ENTRYPOINT ["/app/api"]
    
    1. command docker-compose down and then docker-compose up --build
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 使用 java+selenium 接管手动打开的IE浏览器
  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?