首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改docker基础图像

更改docker基础图像
EN

Stack Overflow用户
提问于 2021-02-25 17:00:40
回答 1查看 36关注 0票数 0

我正在使用图像firesh/nginx-lua..。基础镜像是高山,它附带了一个包管理器apt。我想用一个不同的库来运行这个镜像,所以包管理器将是apt或apt-get。如果我用一个新的Dockerfile编写一个新的Dockerfile,这是一种实现这一目标的方法吗

代码语言:js
复制
FROM firesh/nginx-lua

另一种解决方案是使用lua-nginx的另一个镜像和luarocks包管理器buit。但在码头中心找不到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-25 17:13:29

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

有了这个概念,您可以使用多个FROM在您的Dockerfile中。每个FROM可以使用不同的基础映像。你需要通过上面的文档来了解多阶段构建,有了这个文档,你就可以使用你只需要在最终映像中使用的东西。

从文档开始:

对于多阶段构建,您可以在Dockerfile中使用多个FROM语句。每个FROM指令可以使用不同的基础,并且每个指令都开始一个新的构建阶段。您可以有选择地将工件从一个阶段复制到另一个阶段,从而在最终图像中保留您不想要的所有内容。为了说明这是如何工作的,让我们调整上一节中的Dockerfile以使用多阶段构建。

例如:

代码语言:javascript
复制
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"]

另一个带有注释的示例:

代码语言:javascript
复制
#-------------- 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"]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66365340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档