【问题标题】:How to resolve dockerfile, and docker-compose when main go files are in nested directory?当主要 go 文件位于嵌套目录中时,如何解析 dockerfile 和 docker-compose?
【发布时间】:2021-09-19 00:21:25
【问题描述】:

我对 Docker 很陌生,所以请原谅我,但是当 Main.go 位于根级别时,我有一个工作的 dockerfile 和 docker-compose,但是在这个项目中,如果我将 main.go 放在根级别,应用程序将会中断.

文件结构

queue-backend
- .idea
- cmd
  - appCode
    - handler.go
    - helper.go
    - main.go
    - routes.go
- pkg
  - forms
  - models
    - mongodb
    - models.go
- tmp
.gitignore
docker-compose.yml
Dockerfile
go.mod
README.md
db
extensions

无论如何...我的 dockerfile 看起来像这样

FROM golang:1.16

WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download

COPY ../.. .
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

CMD ["air"]

Docker-compose.yml 看起来像

version: '3.9'
services:
  backend:
    build: ""
    ports:
      - 8000:8000
    volumes:
      - .:/app
    depends_on:
      - mongodb_container
  mongodb_container:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: queue-delivery
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db

volumes:
  mongodb_data_container:

我尝试将 WORKDIR 设置为 /app/cmd/appCode/cmd/appCode 并在 docker-compose 中将其匹配为 .:/app/cmd/appCode.:/cmd/appCode 都不起作用,它总是返回这个,或者上面声明的路径而不是只是“/app”路径

backend_1            | no Go files in /app
backend_1            | failed to build, error: exit status 1

在这一点上,我不确定还能尝试什么...

【问题讨论】:

标签: docker go docker-compose dockerfile


【解决方案1】:

要解析 docker-compose.yml 中的 Dockerfile,您需要更改构建部分,如下所示

version: '3.9'
services:
    backend:
        build:
            context: .
            dockerfile: Dockerfile
        ports:
            - 8000:8000
        volumes:
            - .:/app
        depends_on:
            - mongodb_container

    mongodb_container:
        image: mongo:latest
        environment:
            MONGO_INITDB_ROOT_USERNAME: queue-delivery
            MONGO_INITDB_ROOT_PASSWORD: password
        ports:
            - 27017:27017
        volumes:
            - mongodb_data_container:/data/db

volumes:
    mongodb_data_container:

您的 Dockerfile 有一些问题,

FROM golang:1.16

WORKDIR /app
# File changes must be added at the very end, to avoid the installation of dependencies again and again
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
COPY go.mod .
COPY go.sum .  # can not find this file in the directory structure
RUN go mod download

COPY ../.. .  # doesn't make sense, just use COPY . .

CMD ["air"]

【讨论】:

  • 所以当我运行“docker-compose up --build”时收到backend_1 | can't load package: package .: no Go files in /app backend_1 | failed to build, error: exit status 1 ,我需要更改吗? WORKDIR?
  • 固定工作目录,无视,谢谢您的回答! :~)
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多