docker - 我可以将 Docker Compose 与使用构建器模式的图像一起使用吗?

标签 docker docker-compose

我知道新的 multi-stage build功能,它可以很好地与 Docker Compose 配合使用。但是,假设我坚持使用构建器模式(不要问)......有没有办法让 docker-compose up使用构建器模式所需的构建脚本?

考虑链接文章中的相同构建器模式文件:

Dockerfile.build

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 go get -d -v golang.org/x/net/html \
  && 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 app .
CMD ["./app"]

编译文件
#!/bin/sh
docker build -t alexellis2/href-counter:build . -f Dockerfile.build

docker create --name extract alexellis2/href-counter:build  
docker cp extract:/go/src/github.com/alexellis/href-counter/app ./app  
docker rm -f extract

docker build --no-cache -t alexellis2/href-counter:latest .
rm ./app

我可以构建一个类似这样的 Docker Compose 文件,但我不知道如何 cp来自临时 Docker 容器的文件。

docker-compose.yml
version: '3'
services:
  app: 
    build: .
    depends_on:
     - app-build
  app-build:
    build:
      context: .
      dockerfile: Dockerfile.build

我可以构建临时 Docker 镜像/容器并运行 cp通过使用 build.sh 的第一部分脚本,然后使用精简的撰写文件,但是,那么,我还不如坚持使用脚本。

最佳答案

据我了解,您问是否可以使用类似 docker cp 的东西在撰写文件中从临时容器中提取工件。

好吧,使用共享 volume是一个选项。

version: '3'
    services:
      app: 
        build: .
        depends_on:
         - app-build
        volumes:
        - shared:/source        
      app-build:
        build:
          context: .
          dockerfile: Dockerfile.build
        volumes:
        - shared:/output
    volumes:
      - shared:

但是你必须在app中添加一个脚本服务将检查是否 app-build已完成其工作,工件已准备就绪。

使用共享卷有点冒险。你必须知道你在做什么。

Multiple containers can also share one or more data volumes. However, multiple containers writing to a single shared volume can cause data corruption. https://docs.docker.com/engine/tutorials/dockervolumes/#important-tips-on-using-shared-volumes



github 上有一个标题为“将 copy 添加到 yaml 配置”的问题:https://github.com/docker/compose/issues/1664

我想在撰写文件中有这样的选项。您可以在上面的链接中查看其他人对此的看法。该问题现已关闭,但可能会再次打开。

关于docker - 我可以将 Docker Compose 与使用构建器模式的图像一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152216/

相关文章:

java - 通信链路失败 : Spring boot, MySQL、Docker

php - docker-compose 中的代码接收 - 无法连接到 Webdriver

docker - 在 Windows Server 2019 上为 Docker 拉取 Windows 镜像存在操作系统兼容性问题

Docker-compose:在多个主机中部署服务

docker - docker-compose中的DNS,映射到非80端口?

docker-compose 卷即使从初始化也是空的

docker - 连接被拒绝-使用graphaware连接Neo4j/ES Docker容器

node.js - 如何在使用 docker-compose 和非 root 用户时将 node_modules 保留在容器中?

docker - 与用户一起运行 docker 容器

python - 带有 Google 容器注册表的 Docker SDK