我在" docker-compose"期间添加了RUN go get
来安装软件包。但是,当我运行cannot find package
时,发生了以下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
搬运工-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
Dockerfile
FROM golang:latest
RUN apt-get update
RUN apt-get upgrade -y
ENV GOBIN /go/bin
RUN go get github.com/go-sql-driver/mysql
main.go
package main
import (
"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.go
CHANGELOG.md connection_test.go packets_test.go
CONTRIBUTING.md const.go result.go
LICENSE driver.go rows.go
README.md driver_go18_test.go statement.go
appengine.go driver_test.go statement_test.go
benchmark_go18_test.go dsn.go transaction.go
benchmark_test.go dsn_test.go utils.go
buffer.go errors.go utils_go17.go
collations.go errors_test.go utils_go18.go
connection.go fields.go utils_go18_test.go
connection_go18.go infile.go utils_test.go
更新2
正如@aerokite所说,"卷"覆盖了下载的软件包。我改变了如下并且有效。
Dockerfile
version: '3.4'
FROM golang:latest
RUN apt-get update
RUN apt-get upgrade -y
ENV GOBIN /go/bin
RUN go get github.com/go-sql-driver/mysql
RUN 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
go build -i -o /go/bin/explorer-cli src/explorer-cli/main.go
答案 0 :(得分:6)
我试图重新创建你的问题。
FROM golang:latest
RUN apt-get update
RUN apt-get upgrade -y
ENV GOBIN /go/bin
RUN 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
覆盖泊坞窗中的目录./src
。这是从go get github.com/go-sql-driver/mysql
你明白吗?
但是当你运行go get github.com/go-sql-driver/mysql
时,它现在又重新获取数据。
解决方案(01):
将您的本地卷安装到其他位置。
volumes:
- ./src:/tmp/src
修改您的Dockerfile以将此main.go
移至/go/src
解决方案(02):
将main.go
复制到您的泊坞窗中。在Dockerfile中添加这一行
COPY ./src/main.go /go/src