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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵