gpt4 book ai didi

docker - 创建在Dockerfile中用作FROM时从目录复制文件夹的Docker镜像

转载 作者:行者123 更新时间:2023-12-02 19:48:23 25 4
gpt4 key购买 nike

我想创建一个可以在其他Dockerfile中使用的Docker镜像来构建这些项目。因此,我需要将文件从特定项目的上下文复制到所构建的容器中。

docker是否可以创建可在其他Dockerfile中使用的镜像,并在构建特定于项目的Dockerfile时执行该镜像的Dockerfile中指定的命令?还是对此有其他解决方案?像"template"镜像或Dockerfiles。

我想要实现的是一个可以在我的项目Dockerfile中简单指定的镜像,它可以构建当前项目。当我更新每个项目的图像时,都应该更新。

最佳答案

您所寻找的称为multi-stage builds,并且您可能会在文档中找到一整页的内容。

通常,您可以在上层阶段使用它来构建依赖关系,以便以后使用。

这是文档的示例之一:

FROM golang:1.7.3 AS builder 
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=builder /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]


资料来源: https://docs.docker.com/develop/develop-images/multistage-build/#name-your-build-stages

以及进一步的文档:

Use an external image as a “stage”

When using multi-stage builds, you are not limited to copying from stages you created earlier in your Dockerfile. You can use the COPY --from instruction to copy from a separate image, either using the local image name, a tag available locally or on a Docker registry, or a tag ID. The Docker client pulls the image if necessary and copies the artifact from there. The syntax is:

COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf


因此,您可以从Dockerfile中明确拥有自己的镜像,并使用以下命令进行构建:
docker build . -t my-namespace/template

然后在“应用程序” Dockerfile中复制所需的文件:
FROM alpine
COPY --from=my-namespace/template:latest /path/from/template /path/in/app

关于docker - 创建在Dockerfile中用作FROM时从目录复制文件夹的Docker镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com