我正在使用来自 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 exists but should not
ERROR: Service 'delivery' failed to build: The command '/bin/sh -c go get github.com/go-redis/redis/v7' returned a non-zero code: 1
.
但是,我可以使用 dockerfile 中的图像创建一个 docker 容器docker container run -it --rm golang:1.14.2
,然后运行与 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