进入Dockerfile我找不到包裹错误

【字号: 日期:2024-04-06浏览:38作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决进入Dockerfile我找不到包裹错误?

我试图重现您的问题。

FROM golang:latestRUN apt-get updateRUN apt-get upgrade -yENV GOBIN /go/binRUN go get github.com/go-sql-driver/MysqL

您已提供此Dockerfile。我已经建好了

$ docker build -t test .

现在我执行此图像以运行您的go build命令。

$ docker run -it test bash

然后,我main.go在/go/src目录中创建了您提供的内容。

最后,我已经成功构建,没有任何错误

$ go build -i -o /go/bin/explorer-cli src/main.go

而且我想我已经找到您的问题了。我没用过docker-compose。但是你会明白的。

在这里:

app: build: . tty: true image: explorer-cli:latest container_name: explorer-cli volumes: - ./src:/go/src <-- problem is here external_links: - database

您安装./src到/go/src目录中的码头工人。这个过程是/go/src用您的本地覆盖docker中的目录./src。这正在删除您从中获得的数据goget github.com/go-sql-driver/MysqL

你理解吗?

但是,当您运行时go get github.com/go-sql-driver/MysqL,它现在又可以获取数据。

将本地卷挂载到其他地方。

volumes: - ./src:/tmp/src

并修改您的Dockerfile将其main.go移至/go/src

复制main.go到您的码头工人。在Dockerfile中添加这一行

copY ./src/main.go /go/src解决方法

我添加RUN go get了在“ docker-compose”期间安装软件包。但是,cannot findpackage当我运行时发生了以下错误go build。我发现程序包保存在中/go/pkg/linux_amd64/。

运行docker-compose并进行构建

$ docker-compose up -d$ docker exec -it explorer-cli /bin/bash# pwd/go# ls bin pkg src# echo $GOPATH/go# ls /go/pkg/linux_amd64/github.com/go-sql-driver# go build -i -o /go/bin/explorer-cli src/main.go src/main.go:6:2: cannot find package 'github.com/go-sql-driver/mysql' in any of: /usr/local/go/src/github.com/go-sql-driver/mysql (from $GOROOT) /go/src/github.com/go-sql-driver/mysql (from $GOPATH)(it worked if I run 'go get' manually)# go get github.com/go-sql-driver/mysql# ls src/github.com main.go# go build -i -o /go/bin/explorer-cli src/main.go

docker-compose.yml

version: ’3.4’services: mysql: image: mysql:latest container_name: database volumes: - ./docker/:/etc/mysql/conf.d - ./docker/:/docker-entrypoint-initdb.d environment: - MYSQL_RANDOM_ROOT_PASSWORD=true - MYSQL_DATABASE=explorer - MYSQL_USER=admin - MYSQL_PASSWORD=12dlql*41 app: build: . tty: true image: explorer-cli:latest container_name: explorer-cli volumes: - ./src:/go/src external_links: - database

Docker文件

FROM golang:latestRUN apt-get updateRUN apt-get upgrade -yENV GOBIN /go/binRUN go get github.com/go-sql-driver/mysql

main.go

package mainimport ( 'database/sql' _ 'github.com/go-sql-driver/mysql')func main() { db,err := sql.Open('mysql','admin:12dlql*41@(database:3306)/explorer') if err != nil {panic(err.Error()) } defer db.Close()}

更新1

我注意到以下目录之间的巨大差异。

# ls /go/pkg/linux_amd64/github.com/go-sql-driver/mysql.a# ls /go/src/github.com/go-sql-driver/mysql/AUTHORS connection_go18_test.go packets.goCHANGELOG.md connection_test.go packets_test.goCONTRIBUTING.md const.goresult.goLICENSE driver.go rows.goREADME.md driver_go18_test.go statement.goappengine.go driver_test.go statement_test.gobenchmark_go18_test.go dsn.go transaction.gobenchmark_test.go dsn_test.go utils.gobuffer.go errors.go utils_go17.gocollations.go errors_test.go utils_go18.goconnection.go fields.go utils_go18_test.goconnection_go18.go infile.go utils_test.go

更新2

正如@aerokite所说,“卷”正在覆盖下载的软件包。我像以下内容进行了更改,并且有效。

Docker文件

version: ’3.4’FROM golang:latestRUN apt-get updateRUN apt-get upgrade -yENV GOBIN /go/binRUN go get github.com/go-sql-driver/mysqlRUN mkdir /go/src/explorer-cli

码头工人组成

services: mysql: image: mysql:latest container_name: database volumes: - ./docker/:/etc/mysql/conf.d - ./docker/:/docker-entrypoint-initdb.d environment: - MYSQL_RANDOM_ROOT_PASSWORD=true - MYSQL_DATABASE=explorer - MYSQL_USER=admin - MYSQL_PASSWORD=12dlql*41 app: build: . tty: true image: explorer-cli:latest container_name: explorer-cli volumes: - ./src/explorer-cli:/go/src/explorer-cli external_links: - database

去建造

go build -i -o /go/bin/explorer-cli src/explorer-cli/main.go

相关文章: