go get in Dockerfile. I got cannot find package error

你离开我真会死。 提交于 2019-12-06 09:55:24

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!