登录
首页 >  Golang >  Go问答

$GOPATH/go.mod 存在,但在构建 docker 容器时不应存在,但如果我手动运行命令,则可以使用

来源:stackoverflow

时间:2024-04-13 09:18:34 334浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《$GOPATH/go.mod 存在,但在构建 docker 容器时不应存在,但如果我手动运行命令,则可以使用》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

我正在使用 dockerfile 中的 go-redis 构建一个 golang:1.14.2 docker 容器。

from golang:1.14.2

# project setup and install go-redis
run mkdir -p /go/delivery && cd /go/delivery && \
    go mod init example.com/delivery && \
    go get github.com/go-redis/redis/v7 

# important to copy to /go/delivery
copy ./src /go/delivery

run ls -la /go/delivery

run go install example.com/delivery
entrypoint ["delivery"]

但是,当我尝试使用 docker-compose up --build -d 构建容器时,出现此错误: $gopath/go.mod 存在但不应该 错误:服务“交付”无法构建:命令“/bin/sh -c go get github.com/go-redis/redis/v7”返回非零代码:1

但是,我可以使用 dockerfile docker 容器 run -it --rm golang:1.14.2 中的映像创建一个 docker 容器,然后运行与 dockerfile 中完全相同的命令,并且 delivery 执行我期望的操作。 ``

这里是 deliver.go:

package main

import (
    "fmt"

    "github.com/go-redis/redis/v7"
)

func main() {
    // redis client created here... 

    fmt.println("inside main...")
}

我做错了什么?我查找了此错误消息,但我见过的解决方案都不适合我。

编辑:这是撰写文件:

version: '3.4'
services:
  ...
  delivery:
    build: ./delivery
    environment:
      - REDIS_PORT=${REDIS_PORT}
      - REDIS_PASS=${REDIS_PASS}
      - QUEUE_NAME-${QUEUE_NAME}
    volumes: 
      - ./logs:/logs

解决方案


我也有同样的问题。您需要设置WORKDIR /go/delivery

终于介绍完啦!小伙伴们,这篇关于《$GOPATH/go.mod 存在,但在构建 docker 容器时不应存在,但如果我手动运行命令,则可以使用》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>