进入Dockerfile。我找不到包裹错误 [英] go get in Dockerfile. I got cannot find package error

查看:127
本文介绍了进入Dockerfile。我找不到包裹错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了 RUN go get 在 docker-compose期间安装软件包。但是,当我运行 go build 时,发生以下找不到软件包的错误。我发现这些软件包保存在 / go / pkg / linux_amd64 / 中。

I added RUN go get to install packages during "docker-compose". However, the following cannot find package error was occurred when I run go build. I found that the packages are saved in /go/pkg/linux_amd64/.

运行docker-撰写并进行构建

$ 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

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


Update 1

我注意到以下目录之间存在很大差异。

I noticed big differences between the following directories.

# 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


Update 2

正如@aerokite所说,卷正在覆盖下载的软件包。我像以下内容一样进行了更改,并且可以正常工作。

As @aerokite said, the "volumes" was overwriting the downloaded packages. I changed like the followings and it worked.

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

docker-compose

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


推荐答案

我试图重新创建您的问题。

I have tried to recreate your problem.

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。我已经构建了它

You have provided this Dockerfile. I have build it

$ docker build -t test .

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

Now I exec into this image to run your go build command.

$ docker run -it test bash

然后我在 / go / src中创建了 main.go 目录。

Then I have created main.go, you provided, in /go/src directory.

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

And finally, I have built successfully without any error

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

我想我已经找到您的问题了。我从未使用过 docker-compose 。但是您会明白的。

And I think I have found your problem. I have never used docker-compose. But you will understand.

问题在这里:

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

您正在安装 ./ src 到Docker中的 / go / src 目录中。此过程将用本地 ./ src 覆盖docker中的目录 / go / src 。这就是删除您从获得的数据,去获取github.com/go-sql-driver/mysql

You are mounting ./src into /go/src directory in your docker. This process is overwriting directory /go/src in your docker with your local ./src. And this is removing data you got from go get github.com/go-sql-driver/mysql

但是当您运行 go时,请获取github.com/go-sql-driver/mysql ,现在再次获取数据。

But when you are running go get github.com/go-sql-driver/mysql, its now getting data again.

解决方案(01):

安装您的本地卷到其他地方。

Mount your local volume into somewhere else.

volumes:
  - ./src:/tmp/src

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

And modify your Dockerfile to move this main.go to /go/src

解决方案(02):

main.go 复制到您的Docker中。在Dockerfile中添加此行

Copy main.go into your docker. Add this line in Dockerfile

COPY ./src/main.go /go/src

这篇关于进入Dockerfile。我找不到包裹错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆