简体   繁体   English

在 Dockerfile 和 docker-compose.yml 中编写什么代码以在 docker 环境中传递本地包

[英]What to code in Dockerfile and docker-compose.yml to pass local package in docker environment

I introduced local package with go modules for server side api.我为服务器端 api 引入了带有go modules local package
It works fine in local environment without error by commanding go run main.go .通过命令go run main.go可以在本地环境中正常工作而不会出错。

But it does not work when commanding docker-compose up .但是在命令docker-compose up时它不起作用。

I want to know what to code Dockerfile and docker-compose.yml to fix.我想知道如何编码 Dockerfile 和 docker-compose.yml 来修复。

I command go mod init at article directory.我在article目录下命令go mod init

As result, it sets module github.com/jpskgc/article in go.mod .结果,它在go.mod设置了module github.com/jpskgc/article go.mod

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

main.go 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 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 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.我希望服务器端 api 在 docker 环境中正常工作而不会出错。
But the actual is that docker environment for server side fails to run.但实际情况是服务器端的docker环境无法运行。

Here is error message when commanding docker-compose up这是命令 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).这是github上的完整源代码(分支:try-golang-mod)。 I wish someone to give me answer code for Dockerfile and docker-compose.yml .我希望有人给我Dockerfiledocker-compose.yml答案代码。 https://github.com/jpskgc/article/tree/try-golang-mod https://github.com/jpskgc/article/tree/try-golang-mod

Your docker context is ./api, but the go.mod is in the parent directory, so go.mod is not included in your image.您的 docker 上下文是 ./api,但 go.mod 在父目录中,因此 go.mod 不包含在您的图像中。

One option could be to change your docker context to the root directory of the source tree (parent of api), and copy the files/directories into the image from there.一种选择是将您的 docker 上下文更改为源树的根目录(api 的父目录),并将文件/目录从那里复制到图像中。

I resolve this issue in following way.我通过以下方式解决了这个问题。

  1. go mod init article/api at api directory. go mod init article/api api目录中的go mod init article/api
article
  ├ client
  ├ api
  │  ├ main.go
  │  ├ contoroller
  │  │    └ contoroller.go
  │  ├ //
  │  ├ go.mod 
  │  ├ go.sum
  │  └ Dockerfile
  ├ nginx
  ├ go.mod
  ├ go.sum
  └ docker-compose.yml
  1. fix Docekrfile修复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命令docker-compose down然后docker-compose up --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM