静态转到二进制文件与Docker - 找不到入口点 [英] Static Go Binaries w/ Docker - Entrypoint Not Found

查看:163
本文介绍了静态转到二进制文件与Docker - 找不到入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 Static Go Binaries与OSX上的Docker 一起由Nicola Paolucci尝试使用带Docker的静态Go二进制文件。我相信我正确地遵循了每一步,但是当我运行最终图像时,我得到了来自Docker的以下错误响应。


$ b

注意名称我的服务和可执行文件是 netverify

  docker:来自守护进程的错误响应:找不到或不存在容器命令'/ netverify'.. 

我的 Dockerfile.static 看起来像下面这样...

 #创建一个最小容器来运行Golang静态二进制

FROM tianon / true
ADD netverify /
EXPOSE 8282
CMD [/ netverify]

我的 Dockerfile.build 如下所示...

  FROM golang 

ADD Makefile /
WORKDIR /
RUN make setup

ADD。 /go/src/github.com/eirwin/netverify

RUN make buildgo
CMD [/ bin / bash]

我的 Makefile 如下...



<$ (GOCMD)build
GOGET = $(GOCMD)get -v
GOCLEAN = $(GOCMD)clean
GOINSTALL = $(GOCMD)安装
GOTEST = $(GOCMD)测试
$ b .PHONY:全部

全部:构建

设置:
$(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


$ b $为了这篇文章的目的,我们假设我有以下作为我的golang应用程序。
$ b

  func main() (GET)
http.ListenAndServe(/ ping,api.PingHandler) :8282,router)
}

当我运行 Make运行除了运行图像时,一切似乎都能正常工作。



我可以看到图像在〜8.5MB时正确生成

  eirwin / netverify最新eae16e146b91 3秒前8.63 MB 

但当If docker run -p 8282:8282 eirwin / netverify 运行时,出现以下错误...

  docker:来自守护进程的错误响应:容器命令'/ netverify'未找到或不存在.. 


解决方案

你的makefile中的go static构建缺少一些选项。

  buildgo:
CGO_ENABLED = 0 GOOS = linux go build -ldflags-s-a -installsuffix cgo - o netverify ./go/src/github.com/eirwin/netverify

构建过程可以捕获容器ID以避免计时问题。

分离构建和二进制图像的标签。

  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。

您的二元 Dockerfile.static 可以以 scratch 空白图片。

 从头开始


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

这篇关于静态转到二进制文件与Docker - 找不到入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆