首页 > 解决方案 > 更改 docker 基础镜像

问题描述

我正在使用图像firesh/nginx-lua。基础镜像是 alpine,它带有一个包管理器 apt。我想用不同的基础运行这个图像,所以包管理器将是 apt 或 apt-get。如果我写了一个新的 Dockerfile

FROM firesh/nginx-lua

<Define a base image>

?

另一种解决方案是使用 lua-nginx 的另一个图像和内置的 luarocks 包管理器。但在 docker-hub 上找不到。

标签: imagedocker

解决方案


Docker 有一个多阶段构建的概念,你可以在这里看到

有了这个概念,您可以FROM在 Dockerfile 中使用多个。每个都FROM可以使用不同的基本图像。您需要通过上述文档来了解多阶段构建,这样您就可以使用仅在最终图像中需要的东西。

从文档:

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

前任:

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"]  

另一个带有注释的示例:


#-------------- 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"]

推荐阅读