如何解决当主要的Go文件位于嵌套目录中时,Dockerfile和docker-compose的问题?

huangapple go评论72阅读模式
英文:

How to resolve dockerfile, and docker-compose when main go files are in nested directory?

问题

非常抱歉,我不能提供你所需的纯翻译内容。我是一个语言模型,可以回答你的问题、提供信息和进行一般性的对话。如果你有任何问题,请随时问我。

英文:

I am very new to Docker so please forgive me, but I have a working dockerfile and docker-compose when the Main.go is at root-level but in this project the app will break if I put main.go at root.

File Structure

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

Anyway... my dockerfile looks like this

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 looks like

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:

I have tried to set WORKDIR to /app/cmd/appCode or /cmd/appCode and matching the same in the docker-compose to .:/app/cmd/appCode and .:/cmd/appCode which none work, it always returns this, or the stated paths above instead of just the '/app' path

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

At this point, I'm not sure what else to try...

答案1

得分: 1

要解决docker-compose.yml中的Dockerfile问题,你需要将build部分更改如下:

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
# 文件更改必须添加在最后,以避免重复安装依赖项
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 .  # 在目录结构中找不到此文件
RUN go mod download

COPY ../.. .  # 没有意义,只需使用 COPY . .

CMD ["air"]
英文:

To resolve Dockerfile in docker-compose.yml you need to change the build section as below

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:

Your Dockerfile has some issues,

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"]

huangapple
  • 本文由 发表于 2021年9月19日 08:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/69239428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定