donglou1866 2018-01-29 09:33
浏览 110
已采纳

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

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/.

run docker-compose and go build

$ 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()
}

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

Update 2

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

go build -i -o /go/bin/explorer-cli src/explorer-cli/main.go
  • 写回答

1条回答 默认 最新

  • duannaoben8011 2018-01-29 10:18
    关注

    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
    

    You have provided this Dockerfile. I have build it

    $ docker build -t test .
    

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

    $ docker run -it test bash
    

    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
    

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

    Problem is here:

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

    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

    Do you understand?

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

    Solution (01):

    Mount your local volume into somewhere else.

    volumes:
      - ./src:/tmp/src
    

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

    Solution (02):

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

    COPY ./src/main.go /go/src
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?