问题

有时候开发的go project会用到一些私有仓库,或者自己搭的。比如用到了github.ibm.com里面的代码,如果在go.mod里面设置的 是github.ibm.com的网址的话,会发现build docker image的时候,提示access rights的问题。

方法

在你的Dockerfile,加入这部分,主要是提供了两个arg,方便build image的时候提供你的帐号和密码

# These credentials are needed for the "go mod download" step in order to
# download dependencies from repositories at the github.ibm.com server.
ARG githubUser
ARG githubToken
RUN echo "machine github.ibm.com login ${githubUser} password ${githubToken}" > ~/.netrc \
    && GOPRIVATE=github.ibm.com go mod download \
    && rm ~/.netrc

当然build image的时候要提供上正确的

docker build --build-arg githubUser=xxx@cn.ibm.com --build-arg githubToken=xxx -t your_tag .

这样的话,在go mod download 的时候,才能从github.ibm.com下载到代码。