building golang using your docker image - docker

Good day everyone. I'm in the middle of a task using our docker image to build go microservice. To reduce build time, keep the images under control.
I expect that after downloading the golang image from the docker hub, upload it to our registry and use it in all the dockerfiles of our microservices.
As soon as I started building go mircoservice, during the installation phase go gave me an error:
11:04:16 [91m / bin / sh: go: not found
11:04:16 [0mThe command '/ bin / sh -c go install -mod vendor' returned a non-zero code: 127
I tried to install go in a container, write $ PATH for go, but nothing helped. Then I was told that this image already contains go.
Here are the complete build steps:
11:04:15 Step 1/7 : FROM 192.168.0.8:5000/golang
11:04:15 ---> 49f356fa4513
11:04:15 Step 2/7 : EXPOSE 80
11:04:15 ---> Using cache
11:04:15 ---> 6e0c49e600e6
11:04:15 Step 3/7 : WORKDIR /go/src/autocomplete-trains
11:04:15 ---> Using cache
11:04:15 ---> 41cccfb1ed82
11:04:15 Step 4/7 : COPY . .
11:04:15 ---> 1a44653382a8
11:04:15 Step 5/7 : RUN chmod +x ./start.sh
11:04:15 ---> Running in c13b5cfbfd0b
11:04:16 Removing intermediate container c13b5cfbfd0b
11:04:16 ---> ab03d13e5984
11:04:16 Step 6/7 : RUN go install -mod vendor
11:04:16 ---> Running in b3727493f938
11:04:16 [91m/bin/sh: go: not found
11:04:16 [0mThe command '/bin/sh -c go install -mod vendor' returned a non-zero code: 127
11:04:17 Build step 'Execute shell' marked build as failure
Dockerfile:
FROM 192.168.0.8:5000/golang
EXPOSE 80
WORKDIR /go/src/autocomplete-trains
COPY . .
RUN chmod +x ./start.sh
RUN go install -mod vendor
ENTRYPOINT ./script.sh
Note: If FROM 192.168.0.8:5000/golang:1.15.5-alpine is changed to FROM golang then everything works (but we don't wish to rely upon dockerhub so this is not a viable solution).
The process used to push the image into our repository was:
sudo docker pull golang
sudo docker tag golang:latest 192.168.0.8:5000/golang:1.15.5-alpine
sudo docker push 192.168.0.8:5000/golang:1.15.5-alpine

Related

Docker build not using cache

When I run
docker build -f docker/webpack.docker services/webpack --build-arg env=production
twice in a row, Docker builds my image each time, starting from the first RUN (the COPY uses the cache).
FROM node:lts
ARG env=production
ENV NODE_ENV=$env
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production=false --non-interactive
COPY . .
RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress
How can I get it to cache those RUNs?
Output looks like:
Sending build context to Docker daemon 3.37MB
Step 1/9 : FROM node:lts
---> 0c601cba9f11
Step 2/9 : ARG env=production
---> Using cache
---> dd38b2167c75
Step 3/9 : ENV NODE_ENV=$env
---> Using cache
---> 800f5afd416c
Step 4/9 : WORKDIR /app
---> Using cache
---> d15b93dce11d
Step 5/9 : COPY package.json yarn.lock ./
---> Using cache
---> a049dd1609a8
Step 6/9 : RUN yarn install --frozen-lockfile --production=false --non-interactive
---> Using cache
---> d5e51b0d556c
Step 7/9 : COPY . .
---> 92990e326d4b
Step 8/9 : RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
---> Running in a23878db7b0e
Wrote assets/markons/markons.css
Wrote assets/markons/markons.js
Wrote assets/markons/markons.html
Wrote assets/markons/markons-chars.json
Wrote assets/markons/markons.svg
Wrote assets/markons/markons.ttf
Wrote assets/markons/markons.woff
Wrote assets/markons/markons.woff2
Wrote assets/markons/markons.eot
Removing intermediate container a23878db7b0e
---> 3bce79d0ecf0
Step 9/9 : RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress
---> Running in b6d460488950
<s> [webpack.Progress] 0% compiling
...
See the description:
If the contents of all external files on the first COPY command are
the same, the layer cache will be used and all subsequent commands
until the next ADD or COPY command will use the layer cache.
However, if the contents of one or more external files are different,
then all subsequent commands will be executed without using the layer
cache.
So every time the content is changed two last RUN will be executed with no cache. There is no way to control caching yet. Maybe it's a better option to specify volumes?

Go 1.11 unknown import path for own package in Docker build

I am migrating some code to work with Go 1.11 modules, and I am able to build it from the shell but not in Docker.
Relevant Dockerfile sections:
WORKDIR /goscout
COPY ["go.mod", "go.sum", "./"]
RUN GO111MODULE=on go get -u=patch
COPY *.go ./
RUN GO111MODULE=on go build -v -ldflags "-linkmode external -extldflags -static" -o GoScout -a .
When Docker is running the last command in the above excerpt, I get this error:
can't load package: package github.com/triplestrange/StrangeScout/goscout: unknown import path "github.com/triplestrange/StrangeScout/goscout": ambiguous import: found github.com/triplestrange/StrangeScout/goscout in multiple modules:
github.com/triplestrange/StrangeScout/goscout (/goscout)
github.com/triplestrange/StrangeScout v0.3.0 (/go/pkg/mod/github.com/triplestrange/!strange!scout#v0.3.0/goscout)
I don't get this in the shell, so I'm guessing I am not copying some files correctly. But before this command runs I have copied go.mod, go.sum, and *.go, so I don't know what could be missing.
Make sure that you initialized modules properly for your project
go mod init github.com/triplestrange/StrangeScout/goscout
so that the content of your go.mod is
module github.com/triplestrange/StrangeScout/goscout
And then you can use your current Dockerfile without any changes.
There is no need to set GO111MODULE=on since you're running go commands outside of the $GOPATH
➜ docker build -t goscout .
Sending build context to Docker daemon 47.1kB
Step 1/11 : FROM golang:latest AS builder
---> fb7a47d8605b
Step 2/11 : WORKDIR /goscout
---> Running in e9786fe5ab53
Removing intermediate container e9786fe5ab53
---> 6d101e346175
Step 3/11 : COPY ./ ./
---> 7081c0b47dc9
Step 4/11 : RUN go get -d -v ./...
---> Running in 3ce69359ae88
go: finding github.com/go-sql-driver/mysql v1.4.0
go: finding github.com/gorilla/mux v1.6.2
go: downloading github.com/gorilla/mux v1.6.2
go: downloading github.com/go-sql-driver/mysql v1.4.0
Removing intermediate container 3ce69359ae88
...
---> 3df0dbca80e5
Successfully built 3df0dbca80e5
Successfully tagged goscout:latest

How to run go server on docker from binary

I have been trying to make a Dockerfile, that would let me build my go server as binary and then run it either from the scratch image or alpine. The server works fine locally, on macOS 10.13.5, and I made it work when it wasn't from binary on Docker.
I keep getting this error:
standard_init_linux.go:190: exec user process caused "exec format error"
I have been googling around and found something about system architecture. I am not sure how to check if that is the error and/or how to fix it.
Any hints for debugging or possible fix are much appreciated.
My Dockerfile:
FROM golang:1.10.3 as builder
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get -d -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM scratch
ADD main /
CMD ["/main"]
The output:
Building go
Step 1/9 : FROM golang:1.10.3 as builder
---> 4e611157870f
Step 2/9 : WORKDIR /go/src/gitlab.com/main/server
Removing intermediate container 20cd4d66008b
---> 621d9fc02dde
Step 3/9 : COPY . .
---> cab639571baf
Step 4/9 : RUN go get -d -v ./...
---> Running in 7681f9adc7b2
Removing intermediate container 7681f9adc7b2
---> 767a4c9dfb94
Step 5/9 : RUN go build -a -installsuffix cgo -o main .
---> Running in a6ec73121163
Removing intermediate container a6ec73121163
---> b9d7d1c0d2f9
Step 6/9 : FROM alpine:latest
---> 11cd0b38bc3c
Step 7/9 : WORKDIR /app
---> Using cache
---> 6d321d334b8f
Step 8/9 : COPY . .
---> 048a59fcdd8f
Step 9/9 : CMD ["/app/main"]
---> Running in d50d174644ff
Removing intermediate container d50d174644ff
---> 68f8f3c6cdf7
Successfully built 68f8f3c6cdf7
Successfully tagged main_go:latest
Creating go ... done
Attaching to go
go | standard_init_linux.go:190: exec user process caused "exec format error"
go exited with code 1
As #tgogos pointed out did I need to use what I build in the first step.
My final Dockerfile ended like this with a few further improvements: The important part is second last line though:
FROM golang:1.10.3 AS build
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get github.com/golang/dep/cmd/dep && \
dep ensure && \
rm -f schema/bindata.go && \
go generate ./schema
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=build /go/src/gitlab.com/main/server/main .
CMD ["/main"]

When build beego docker image with default docker file, show error: `godep: No Godeps found (or in any parent directory)`

I'm new to Go & Beego.
When I build docker image with beego's default docker file, it shows this error :
godep: No Godeps found (or in any parent directory)
The build info is:
Sending build context to Docker daemon 13.6MB
Step 1/9 : FROM library/golang
---> 138bd936fa29
Step 2/9 : RUN go get github.com/tools/godep
---> Running in 9003355d967f
---> bae9e4289f9b
Removing intermediate container 9003355d967f
Step 3/9 : RUN CGO_ENABLED=0 go install -a std
---> Running in 63d367bd487e
---> 3ce0b2d47c0a
Removing intermediate container 63d367bd487e
Step 4/9 : ENV APP_DIR $GOPATH/src/TestProject
---> Running in 53ddc4661a07
---> 528794352eb0
Removing intermediate container 53ddc4661a07
Step 5/9 : RUN mkdir -p $APP_DIR
---> Running in 37718f358f5c
---> ef9332ca086c
Removing intermediate container 37718f358f5c
Step 6/9 : ENTRYPOINT (cd $APP_DIR && ./TestProject)
---> Running in 059c06321914
---> 8538ea070871
Removing intermediate container 059c06321914
Step 7/9 : ADD . $APP_DIR
---> df129482c662
Step 8/9 : RUN cd $APP_DIR && CGO_ENABLED=0 godep go build -ldflags '-d -w -s'
---> Running in 50b29d1307b5
godep: No Godeps found (or in any parent directory)
The command '/bin/sh -c cd $APP_DIR && CGO_ENABLED=0 godep go build -ldflags '-d -w -s'' returned a non-zero code: 1
The solution is very simple: run godep save in your project locally, and you will go a new folder Godeps in your project. it contains file:Godeps.json. After this, run docker build . again, you will got your docker image.

Docker glide not found

Docker is not able to find glide, which was installed successfully in steps 3 and 4 (below). I ran
docker build .
This is the first part of the Dockerfile:
FROM golang:latest as builder
# Set up workdir
WORKDIR /go/src/github.com/cayleygraph/cayley
# Restore vendored dependencies
RUN sh -c "curl https://glide.sh/get | sh"
COPY glide.* ./
RUN glide install
But it failed on step 5 with this error:
docker build .
Sending build context to Docker daemon 65.18MB
Step 1/29 : FROM golang:latest as builder
---> 1a34fad76b34
Step 2/29 : WORKDIR /go/src/github.com/cayleygraph/cayley
---> Using cache
---> dd9a295edeed
Step 3/29 : RUN sh -c "curl https://glide.sh/get | sh"
---> Using cache
---> b432efdb0630
Step 4/29 : COPY glide.* ./
---> Using cache
---> 936b9f7837eb
Step 5/29 : RUN glide install
---> Running in b244dcff6576
/bin/sh: 1: glide: not found
The command '/bin/sh -c glide install' returned a non-zero code: 127
Installing glide worked, not sure why it's not finding the actual executable though. Any ideas?
Make glide executable as well, in the Dockerfile. And skip the / in your COPY statement. Try to add this before you try to run it.
RUN chmod +x <file>
Seems like the install process only download the executable

Resources