I'm trying to build a docker image from my go
app. The github repo is private. I'm a bit new to go (and docker), but I thought that the /vendor
file managed through glide
should circumvent the problem? Also why does docker have to get everything from github? Everything is available locally.
command:
docker build -t testimage .
Dockerfile
FROM golang:1.8-onbuild
It fails at the step
exec go get -v -d
.......
fatal: could not read Username for 'https://github.com': No such device or address
How can I get around this? Thx
If you have /vendor
inside the root directory of your project/package, it should not pull anything from the internet. Here's how I did mine: https://gist.github.com/orvyl/7000f973abe88d40c8c91d4e28e139a7
Is there anything else in the Dockerfile other than FROM? What docker command are you using to turn on the container? If you're just trying to build a docker image and that's failing, there shouldn't be any interaction or authorization with github unless defined in the Dockerfile.
Can you try running this? It will pull down the official Go Alpine image and boot you into an interactive terminal.
docker run -it golang:1.9.2-alpine3.6 sh
Alternatively you could also use your image.
docker run -it golang:1.8-onbuild sh
If that works, the issue is probably with the Dockerfile and not with Docker or the Go image.
If you want to know more about Docker and Go checkout the official repository. I also recently wrote about Docker and Go and how to get up and running with it quickly.
Only use golang:1.8-onbuild
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.
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.
I'll give you our standard process.
If your local machine is a Mac or Windows, you need to cross-compile
your Go code for linux using: GOOS=liux GOARCH=amd64 go build -o myapp_linux-amd64
. The linux-amd64
is just a convention to remind yourself that the file is compiled for linux, not mac or windows.
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: -a -ldflags '-w -extldflags "-static"'
As an extra, we also remove the developer's own path in the filename listed in a stacktrace using: -gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd)
The resulting compile command that we use is: 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
You can now build your app locally on your dev env and create the image using the following Dockerfile:
FROM alpine:3.6
COPY ./myapp_linux-amd64 /usr/local/bin/myapp
ENTRYPOINT []
CMD /usr/local/bin/myapp
build it using:
docker build -t myimagename:tag .