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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)