更改Docker基本映像 [英] Change a docker base Image

查看:62
本文介绍了更改Docker基本映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图像 firesh/nginx-lua .基本映像是alpine,它附带一个apt软件包管理器.我想使用其他基础来运行此映像,因此包管理器将为apt或apt-get.如果我使用

I am using the Image firesh/nginx-lua. The base image is alpine and it comes with a package manager apt. I would like to run this image with a different base, so the package manager will be apt or apt-get. Is the a way to achieve that if I wrote a new Dockerfile with

FROM firesh/nginx-lua

<Define a base image>

?

另一种解决方案是将lua-nginx的另一个映像与luarocks软件包管理器一起使用.但是在docker-hub上找不到.

Another solution is to use another Image of lua-nginx with luarocks package-manager buit in. But couldn't find one on docker-hub.

推荐答案

Docker具有多阶段构建的概念,您可以看到

Docker has a concept of multi stage builds which you can see here

有了这个概念,您可以在Dockerfile中使用多个 FROM .每个 FROM 都可以使用不同的基本映像.您需要阅读上面的文档,以了解多阶段构建,从而可以仅在最终图像中使用所需的东西.

With this concepts, you can use multiple FROM in your Dockerfile. Each FROM can use a different base image. You need to go through the above doc for learning about multi-stage builds, with this you can use things that you need only in your final image.

从文档开始:

对于多阶段构建,您可以在Dockerfile中使用多个FROM语句.每个FROM指令可以使用不同的基础,并且每个都开始构建的新阶段.您可以有选择地将工件从一个阶段复制到另一个阶段,从而在最终图像中留下不需要的所有内容.为了展示它是如何工作的,让我们改编上一部分中的Dockerfile以使用多阶段构建.

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image. To show how this works, let’s adapt the Dockerfile from the previous section to use multi-stage builds.

例如:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

另一个带有注释的示例:

Another example with comments:


#-------------- building an optimized docker image for the server using multi-stage builds -----------
#--first stage of the multi-stage build will use the golang:latest image and build the application--
# start from the latest golang base image
FROM golang:latest as builder

# add miantainer info
LABEL maintainer="Sahadat Hossain"

# set the current working directory inside the container
WORKDIR /app

# copy go mod and sum files
COPY go.mod go.sum ./

# download all dependencies, dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# build the Go app (API server)
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server .

############ start a new stage from scracthc ###########
FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

# copy the pre-built binary file from the previous stage
COPY --from=builder /app/server .

# Expose port 8080 to the outside world
EXPOSE 8080

# command to run the executable
CMD ["./server", "start"]

这篇关于更改Docker基本映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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