简体   繁体   English

尝试在docker中使用dep安装依赖项

[英]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. 我创建了一个docker文件,其中安装了golang dep工具,该工具将用于安装golang项目所需的依赖项。 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 我不确定如何配置dep工具以能够在docker映像中运行dep命令,它将安装golang项目所需的所有依赖项

I am using below command to run dep tool and it works in local machine 我正在使用下面的命令来运行dep工具,它在本地计算机上工作

# 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 初始化失败:无法确定根项目/ go的导入路径:/ go不在任何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. 有一些教程可以构建docker文件来构建golang项目,但是互联网上没有任何可以使用golang dep工具安装依赖项的东西。

Here is an example of Dockerfile with dep: 这是带有dep的Dockerfile的示例:

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"]

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. 另外,为了获得依赖关系,通常已经有了一个Gopkg.tomlGopkg.lock仅当您从使用另一个供应商工具的项目中迁移时才使用dep init ,根本不供应或从头开始一个项目。

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM