gpt4 book ai didi

docker - 如何将本地包导入docker环境

转载 作者:数据小太阳 更新时间:2023-10-29 03:41:38 29 4
gpt4 key购买 nike

我尝试在 Docker 环境中运行 Go API 服务器,但它运行失败并出现错误。

・client: react/axios
・api: golang/gin
・web-server: nginx
・db: mysql
・container: docker
・ci-tool: travis
・deploy: aws elastic beanstalk
article
├ client
├ api
│ ├ main.go
│ ├ contoroller
│ │ └ contoroller.go
│ └ Dockerfile
├ nginx
└ docker-compose.yml

这是 api 的 Dockefile

//article/api/Dockefile

FROM golang:latest
WORKDIR '/app'
RUN go get github.com/gin-contrib/cors
RUN go get github.com/gin-gonic/gin
RUN go get github.com/go-sql-driver/mysql
RUN go get github.com/google/uuid
RUN go get github.com/aws/aws-sdk-go/aws
RUN go get github.com/aws/aws-sdk-go/service/s3
RUN go get github.com/joho/godotenv
COPY . .
CMD ["go", "run", "main.go"]

这里是docker-compose.yml

//docker-compose.yml
api:
build:
dockerfile: Dockerfile.dev
context: ./api
volumes:
- ./api:/app
depends_on:
- db
tty: true
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- MYSQL_USER
- MYSQL_PASSWORD
- MYSQL_HOST

我希望 docker-compose up 成功,但我失败了。

api_1     | main.go:14:2: cannot find package "github.com/jpskgc/article/api/controller" in any of:
api_1 | /usr/local/go/src/github.com/jpskgc/article/api/controller (from $GOROOT)
api_1 | /go/src/github.com/jpskgc/article/api/controller (from $GOPATH)

最佳答案

为什么不使用 go 模块?

在 Golang 中使用多阶段构建 docker 非常流行。

这将创建一个非常小的图像。

FROM golang:1.12.9-alpine AS builder
WORKDIR /src
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./ ./
RUN CGO_ENABLED=0 go build \
-installsuffix 'static' \
-o /myapp .

FROM scratch AS final
COPY --from=builder /myapp /myapp

EXPOSE 8080

ENTRYPOINT ["/myapp"]

请注意,您无法在正在运行的容器上运行 bash。使用网络服务时需要添加ca-certificates

关于docker - 如何将本地包导入docker环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57606241/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com