<p>I'm trying to build a docker image from my <code>go</code> app. The github repo is private. I'm a bit new to go (and docker), but I thought that the <code>/vendor</code> file managed through <code>glide</code> should circumvent the problem? Also why does docker have to get everything from github? Everything is available locally. </p>

<p>command:</p>

<pre><code>docker build -t testimage .
</code></pre>

<p>Dockerfile</p>

<pre><code>FROM golang:1.8-onbuild
</code></pre>

<p>It fails at the step</p>

<pre><code>exec go get -v -d
....... 
fatal: could not read Username for 'https://github.com': No such device or address
</code></pre>

<p>How can I get around this? Thx</p>

<p>Only use <code>golang:1.8-onbuild</code> if you need to compile your go code image INSIDE a container. That's typically useful for CI builds. Otherwise avoid since it's a massive image.</p>

<p>A much much faster solution is to build your Go application locally (dev env for instance) and copy the final Go application to a very lightweight container.</p>

<p>I'll give you our standard process.</p>

<ol>
<li><p>If your local machine is a Mac or Windows, you need to <code>cross-compile</code> your Go code for linux using: <code>GOOS=liux GOARCH=amd64 go build -o myapp_linux-amd64</code>. The <code>linux-amd64</code> is just a convention to remind yourself that the file is compiled for linux, not mac or windows.</p></li>
<li><p>We also deploy our Go apps to the very lightweight Alpine linux container. Alpine is now the standard Docker image to create app. It's very small and secure but it has one major quirk; it is using the musc instead of the more common glibc as the underlying OS/IO library, so we need a few more compilation flags: <code>-a -ldflags '-w -extldflags "-static"'</code></p></li>
<li><p>As an extra, we also remove the developer's own path in the filename listed in a stacktrace using: <code>-gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd)</code></p></li>
</ol>

<p>The resulting compile command that we use is:
<code>
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOROOT_FINAL=$(pwd) go build -a -ldflags '-w -extldflags "-static"' -gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd) -o myapp_linux-amd64
</code></p>

<p>You can now build your app locally on your dev env and create the image using the following Dockerfile:</p>

<pre><code>FROM alpine:3.6
COPY ./myapp_linux-amd64 /usr/local/bin/myapp

ENTRYPOINT []
CMD /usr/local/bin/myapp
</code></pre>

<p>build it using:</p>

<p><code>docker build -t myimagename:tag .</code></p>
