Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to install dependencies using dep in docker

I have created a docker file in which I have installed golang dep tool which will be used to install the dependencies required by golang project. I have been able to install the tool. But unable to install the dependencies using that tool. I am not sure how to configure dep tool to be able to run dep command in docker image which will install all the dependencies required by golang project

I am using below command to run dep tool and it works in local machine

# initialize the project and install dependencies
RUN dep init

I am always getting an error:

init failed: unable to determine the import path for the root project /go: /go is not within any GOPATH/src

Now I do not know if I need to set path to o binary files or how I can achieve that. There are tutorials to build a docker file to build golang project but nothing is there on internet to install dependencies using golang dep tool.

like image 301
Himanshu Avatar asked Jan 01 '23 19:01

Himanshu


2 Answers

Here is an example of Dockerfile with dep:

FROM golang:latest 

LABEL version="1.0"

RUN mkdir /go/src/app

RUN go get -u github.com/golang/dep/cmd/dep

ADD ./main.go /go/src/app
COPY ./Gopkg.toml /go/src/app

WORKDIR /go/src/app 

RUN dep ensure 
RUN go test -v 
RUN go build

CMD ["./app"]
like image 67
Philidor Avatar answered Jan 05 '23 16:01

Philidor


You need to change the directory to that of your project. Also, in order to get dependencies, you will usually already have a Gopkg.toml and Gopkg.lock - dep init is ONLY used when you're moving from a project which was using another vendoring tool, no vendoring at all or you're starting a project from scratch.

To sum up, I'd do something like this:

FROM golang:latest
RUN go get -u github.com/golang/dep/cmd/dep \
&&  mkdir /go/src/github.com/you \
&&  git clone https://github.com/you/yourproject /go/src/github.com/you/yourproject

WORKDIR /go/src/github.com/you/yourproject

RUN dep ensure -v
&&  go build

CMD ["./yourproject"]
like image 26
morganbaz Avatar answered Jan 05 '23 15:01

morganbaz