小编典典

进入Dockerfile。我找不到包裹错误

go

我添加RUN go get了在“ docker-compose”期间安装软件包。但是,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

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: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所说,“卷”正在覆盖下载的软件包。我像以下内容进行了更改,并且有效。

Docker文件

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 -i -o /go/bin/explorer-cli src/explorer-cli/main.go

阅读 306

收藏
2020-07-02

共1个答案

小编典典

我试图重现您的问题。

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用您的本地覆盖docker中的目录./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
2020-07-02