duanjiao6735 2019-05-04 18:19
浏览 137
已采纳

在Docker中构建干净的Go应用程序

I'm trying to create Go web server into small Docker images. Ideally the clean image contains only the Go application itself (and maybe supporting web components, but not the Go-building environment).

Here is my Dockerfile:

# golang:latest as build-env
FROM golang:latest AS build-env

RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN cd /app && GO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
# go build -o myapp

FROM scratch
COPY --from=build-env /app/myapp /app/images /

EXPOSE 8080
ENTRYPOINT /myapp

It uses the Docker Builder Pattern and scratch image, which is a special docker image that's empty.

It builds OK, but when I run it, I'm getting:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown.

UPDATE:

So the ENTRYPOINT need to be changed to the exec form:

ENTRYPOINT ["/myapp"]

Having done that, I'm getting a new error:

standard_init_linux.go:207: exec user process caused "no such file or directory"

Having use a small footprint Linux image as the base (i.e. Alpine Linux) instead of scratch wouldn't help either:

$ docker run -it -p 8080:8080 go-web-docker-small            
standard_init_linux.go:207: exec user process caused "no such file or directory"

$ docker run -it -p 8080:8080 go-web-docker-small /bin/sh -i 
standard_init_linux.go:207: exec user process caused "no such file or directory"

How to fix it? Thx!

  • 写回答

2条回答 默认 最新

  • douying0108 2019-05-04 19:18
    关注

    While building Dockerfile provided by you, I am getting following error:

    COPY failed: stat /var/lib/docker/overlay2/cc1f8144192760ce7bf9cda7a7dfd0af16065901594c38609c813ea103cfd8d7/merged/app/images: no such file or directory

    Fixed copy command and few others and image is building with following in Dockerfile

    # golang:latest as build-env
    FROM golang:latest AS build-env
    
    RUN mkdir /app
    ADD . /app/
    WORKDIR /app
    RUN cd /app && GO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
    # go build -o myapp
    
    FROM scratch
    COPY --from=build-env /app/myapp .
    
    EXPOSE 8080
    ENTRYPOINT ["./myapp"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条