更改 docker 基础镜像

时间:2021-02-25 09:00:39

标签: image docker

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

编写了一个新的Dockerfile,这是一种实现这一目标的方法吗?
FROM firesh/nginx-lua

<Define a base image>

另一种解决方案是使用 lua-nginx 的另一个 Image 和 luarocks package-manager buit in。但在 docker-hub 上找不到。

1 个答案:

答案 0 :(得分:1)

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

有了这个概念,您可以在 Dockerfile 中使用多个 FROM。每个 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"]