image - 更改 docker 基础镜像
问题描述
我正在使用图像firesh/nginx-lua。基础镜像是 alpine,它带有一个包管理器 apt。我想用不同的基础运行这个图像,所以包管理器将是 apt 或 apt-get。如果我写了一个新的 Dockerfile
FROM firesh/nginx-lua
<Define a base image>
?
另一种解决方案是使用 lua-nginx 的另一个图像和内置的 luarocks 包管理器。但在 docker-hub 上找不到。
解决方案
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"]
推荐阅读
- java - SSLHandshakeErrorTracker : SSLC0008E: 无法初始化 SSL 连接。未经授权的访问被拒绝或安全设置已过期
- java - 如何通过终端运行我的 GitHub 存储库:创建运行文件
- c++ - 模板可以专门用于标准库类而不包括该类的标头吗?
- html - 徽标没有被悬停替换
- sql - Oracle 查询比较两个不同表中的日期
- sql - 如何根据付款方式(例如每月,每季度,每年)制作付款发票
- tensorflow - TensorFlow 图像重塑
- sublimetext3 - “尝试解析设置时出错:Packages\User\EasyClangComplete.sublime-settings 中的预期值:18:1”
- python-2.7 - 从我们的太阳能系统中解码寄存器数据
- mysql - 如何将记录插入到具有用户 ID 作为外键的表中