I used Static Go Binaries with Docker on OSX by Nicola Paolucci to try to use static Go binary w/ Docker. I believe I followed every step correctly, but when I run the final image, I get the following error response from Docker.
NOTE The name of my service and executable are netverify
docker: Error response from daemon: Container command '/netverify' not found or does not exist..
My Dockerfile.static looks like the following...
#Create a minimal container to run a Golang static binary
FROM tianon/true
ADD netverify /
EXPOSE 8282
CMD ["/netverify"]
My Dockerfile.build looks like the following...
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
ADD . /go/src/github.com/eirwin/netverify
RUN make buildgo
CMD ["/bin/bash"]
My Makefile is the following...
GOCMD = go
GOBUILD = $(GOCMD) build
GOGET = $(GOCMD) get -v
GOCLEAN = $(GOCMD) clean
GOINSTALL = $(GOCMD) install
GOTEST = $(GOCMD) test
.PHONY: all
all: build
setup:
$(GOGET) github.com/gorilla/mux
buildgo:
GOOS=linux $(GOBUILD) -o netverify ./go/src/github.com/eirwin/netverify
builddocker:
docker build -t eirwin/netverify -f ./Dockerfile.build .
docker run -t eirwin/netverify /bin/true
docker cp `docker ps -q -n=1`:/netverify .
chmod 755 ./netverify
docker build --rm=true --tag=eirwin/netverify -f Dockerfile.static .
run: builddocker
docker run -p 8282:8282 eirwin/netverify
For the purpose of this post, lets assume I have the following as my golang application.
func main() {
router := mux.NewRouter()
router.HandleFunc("/ping", api.PingHandler).Methods("GET")
http.ListenAndServe(":8282", router)
}
When I run Make run everything seems to work except for when the image is ran.
I can see that the image builds correctly at ~8.5MB
eirwin/netverify latest eae16e146b91 3 seconds ago 8.63 MB
But when If docker run -p 8282:8282 eirwin/netverify is ran I get the following error...
docker: Error response from daemon: Container command '/netverify' not found or does not exist..
The go static build in your makefile is missing some options.
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o netverify ./go/src/github.com/eirwin/netverify
The build process can capture the container ID to avoid timing issues.
Separate the tags for build and binary images.
builddocker:
docker build -t eirwin/netverify-build -f ./Dockerfile.build .
CID=$$(docker create eirwin/netverify-build); \
docker cp $$CID:/netverify .; \
docker rm $$CID
chmod 755 ./netverify
docker build --rm=true --tag=eirwin/netverify -f Dockerfile.static .
Your binary Dockerfile.static can start with the scratch blank image.
FROM scratch
Related
I am trying to build a docker file using docker-compose. Part of the build is related to go language. When I build the code I get the following error. What could be the issue? How do I fix this error ? Could you please advise? I am new to go language
---> Running in 68f94f330d0b
Removing intermediate container 68f94f330d0b
---> 7a9ca17f7bd7
Step 15/16 : RUN go build -o image_srv .
---> Running in 38fa96ef75d6
# golang.org/x/sys/unix
/go/src/golang.org/x/sys/unix/syscall.go:83:16: undefined: unsafe.Slice
/go/src/golang.org/x/sys/unix/syscall_unix.go:118:7: undefined: unsafe.Slice
/go/src/golang.org/x/sys/unix/sysvshm_unix.go:33:7: undefined: unsafe.Slice
The command '/bin/sh -c go build -o image_srv .' returned a non-zero code: 2
You can find appropriate 1.17 tags by going to https://hub.docker.com/_/golang/tags?page=1&name=1.17
Probably golang:1.17.13-alpine will suite your use case
Take note that you may need to delete any existing docker images so that the image can rebuild
# list docker images
$ docker image ls
# delete image foobar
$ docker rmi foobar
I am trying to containerize Go Developer environment.
Blogs I am referring: https://www.docker.com/blog/containerize-your-go-developer-environment-part-1/
I am getting an error:
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = invalid field '' must be a key=value pair
Makefile:8: recipe for target 'bin/example' failed
make: *** [bin/example] Error 1
My Dockerfile:
# syntax = docker/dockerfile:1-experimental
FROM --platform=${BUILDPLATFORM} golang:1.17.8-alpine AS base
WORKDIR /src
ENV CGO_ENABLED=0
COPY go.* .
RUN --mount=type=cache, target=/go/pkg/mod go mod download
FROM base AS build
ARG TARGETOS
ARG TARGETARCH
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
FROM base AS unit-test
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build go test -v .
FROM golangci/golangci-lint:v1.43-alpine AS lint-base
FROM base AS lint
RUN --mount=target=. --mount=from=lint-base, src=/usr/bin/golangci-lint, target=/usr/bin/golangci-lint --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build --mount=type=cache, target=/root/.cache/golangci-lint golangci-lint run --timeout 10m0s ./...
FROM scratch AS bin-unix
COPY --from=build /out/example /
FROM bin-unix AS bin-linux
FROM bin-unix AS bin-darwin
FROM scratch AS bin-windows
COPY --from=build /out/example /example.exe
FROM bin-${TARGETOS} AS bin
Makefile is as follows:
all: bin/example
test: lint unit-test
PLATFORM=linux/amd64
.PHONY: bin/example
bin/example:
#docker build . --target bin --output bin/ --platform ${PLATFORM}
.PHONY: unit-test
unit-test:
#docker build . --target unit-test
.PHONY: lint
lint:
#docker build . --target lint
In terminal:
atin#atin-VirtualBox:~/Desktop/container-go-dev$ export DOCKER_BUILDKIT=1
atin#atin-VirtualBox:~/Desktop/container-go-dev$ make
[+] Building 23.2s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 1.6s
=> => transferring dockerfile: 38B 0.4s
=> [internal] load .dockerignore 1.3s
=> => transferring context: 34B 0.3s
=> resolve image config for docker.io/docker/dockerfile:1-experimental 10.3s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1-experimental#sha2 0.0s
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to create LLB definition: dockerfile parse error line 7: invalid field '' must be a key=value pair
Makefile:8: recipe for target 'bin/example' failed
make: *** [bin/example] Error 1
This is line #7 of your docker file:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
I'm certainly not an expert at dockerfiles but that syntax does not look right to me. I have no idea what program in your image the RUN command is invoking, but I question whether there should be a comma here, and should there be an equals sign between "mount" and "type", or should it be a dash? And, does it even work to pass an option here with "--"?
You have a lot of invalid whitespace throughout your file, including the syntax line up top (likely resulting in it being ignored) and in the --mount options:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
Should be
RUN --mount=type=cache,target=/go/pkg/mod go mod download
i am trying monitor esxi via prometheus to grafana using https://github.com/devinotelecom/prometheus-vmware-exporter
when i am running
[root#admin01 prometheus-vmware-exporter]# docker build -t prometheus-vmware-exporter .
Sending build context to Docker daemon 157.7kB
Step 1/9 : FROM golang:1.11 as builder
---> 43a154fee764
Step 2/9 : WORKDIR /src/github.com/devinotelecom/prometheus-vmware-exporter
---> Using cache
---> 6b2aad4c7a43
Step 3/9 : COPY ./ /src/github.com/devinotelecom/prometheus-vmware-exporter
---> Using cache
---> 2f5f1b155f7f
Step 4/9 : RUN go get -d -v
---> Using cache
---> a48d35b3d5e2
Step 5/9 : RUN CGO_ENABLED=0 GOOS=linux go build
---> Running in 86199cee4fcb
# github.com/prometheus/client_golang/prometheus
/go/src/github.com/prometheus/client_golang/prometheus/build_info_collector.go:24:15: undefined: debug.ReadBuildInfo
The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build' returned a non-zero code: 2
[root#admin01 prometheus-vmware-exporter]#
Thanks for your help in Advanced
The minimal golang version in prometheus is 1.15, but in the Dockerfile it says 1.11
source : https://github.com/prometheus/client_golang/blob/main/go.mod
Try changing golang:1.11 to golang:1.15++ in Dockerfile
I need to create a big docker image with the following steps:
Create Dockerfile with the following info:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1.202-bionic as build
ARG BUILDCONFIG=RELEASE
ARG VERSION=1.0.0
WORKDIR .
COPY . /build/
COPY Model/Model.sln /build/Model/
RUN dotnet build ./build/Model/Model.sln
RUN dotnet restore ./build/Model/Model.sln
WORKDIR /build/Model
RUN dotnet publish Model.sln -c $BUILDCONFIG -o out /p:Version=$VERSION
Build docker image with following command
$ docker build -t modelwithpr .
Sending build context to Docker daemon 27.39GB
Step 1/10 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1.202-bionic as build
3.1.202-bionic: Pulling from dotnet/core/sdk
23884877105a: Pull complete
bc38caa0f5b9: Pull complete
2910811b6c42: Pull complete
36505266dcc6: Pull complete
21e3523d71cd: Pull complete
3c423fdfc300: Pull complete
3c953506ccad: Pull complete
3b8b572de54f: Pull complete
8f2eb870f86d: Pull complete
e52b20b76b9c: Pull complete
Digest: sha256:9ee35c3d8395a15b21cc2eca5d66c8e7df62254c12ddb1249731c568458de124
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1.202-bionic
---> 82688290b31f
Step 2/10 : ARG BUILDCONFIG=RELEASE
---> Running in 74f348453403
Removing intermediate container 74f348453403
---> f07df710b48e
Step 3/10 : ARG VERSION=1.0.0
---> Running in d1556dc14108
Removing intermediate container d1556dc14108
---> 63b311ef2174
Step 4/10 : WORKDIR .
---> Running in acd6a2b9f467
Removing intermediate container acd6a2b9f467
---> cecc23485351
Step 5/10 : COPY . /build/
Error processing tar file(exit status 1): write /build/Model/bin/Debug/netcoreapp3.1/Microsoft.Data.SqlClient.dll: no space left on device
I check the path Library/Containers/com.docker.docker/Data/vms/0/data and find the Docker.raw file is 59GB(don't know why it's so big).
Is there a way to enlarge the container size?
I am getting an error saying "docker: Error response from daemon: OCI runtime create failed" when I give an argument to my Docker image in running.
My application source code follows:
require "open-uri"
require "nokogiri"
crawling_url = ARGV[0]
unless crawling_url
puts "URL to crawl is empty"
exit 1
end
puts crawling_url
page = Nokogiri.HTML(open(crawling_url))
puts page.title
And Dockerfile is:
FROM ruby:2.6
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN gem install bundler
RUN bundle install
COPY . .
CMD ["ruby", "/usr/src/app/crawler.rb"]
The build command I use is:
$ docker build -t crawler .
When I don't give any argument to my script, it works, but when I give one, it doesn't.
$ docker run -it crawler
URL to crawl is empty
$ docker run -it crawler "https://google.com"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"https://google.com\": stat https://google.com: no such file or directory": unknown.
ERRO[0001] error waiting for container: context canceled
What's wrong with it?
It worked after I changed CMD to ENTRYPOINT at last.
ENTRYPOINT ["ruby", "/usr/src/app/crawler.rb"]