docker 在 docker 撰写后提交

docker commit after docker compose

我运行docker作文如下:

docker-compose -f docker-compose.dev.yml up --build -d

docker-compose.dev.yml的内容是:

version: '3'

services:

  client:
    container_name: client
    build:
      context: frontend
    environment:
      - CADDY_SUBDOMAIN=xxx
      - PRIVATE_IP=xxx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    links:
      - express
    volumes:
      - /home/ec2-user/.caddy:/root/.caddy

  express:
    container_name: express
    build: express
    environment:
      - NODE_ENV=development
    restart: always

然后我想通过将这些容器推送到 aws ECR 并拉入测试服务器来从这些容器创建图像以在测试服务器中使用它们,以避免重新创建 dockers 的时间.简单地使用 docker commit 是行不通的。

根据 docker compose 的输出创建图像的正确方法是什么?

谢谢

你基本上不应该使用 docker commit。标准方法是描述如何 build your images using a Dockerfile,并将该文件签入源代码管理。您可以将构建的镜像推送到像 Docker Hub 这样的注册中心,您可以查看原始源代码并重建镜像。

好消息是您基本上已经有了这个设置。您的每个 Compose 服务都有一个 build: 块,其中包含有关如何构建图像的数据。所以就够了

docker-compose build

您将获得每个组件的单独 Docker 图片。

通常,如果您这样做,您还想将图像推送到某个 Docker 注册表。在 Compose 设置中,您也可以为每个服务指定一个 image:。如果您同时拥有 build:image:,则指定要用于构建图像的图像名称(否则 Compose 将根据项目名称选择一个)。

version: '3.8'
services:
  client:
    build:
      context: frontend
    image: registry.example.com/project/frontend
    et: cetera
  express:
    build: express
    image: registry.example.com/project/express
    et: cetera

然后您可以让 Compose 构建和推送图像

docker-compose build
docker-compose push

最后一个有用的技术是将 Compose 设置拆分为两个文件。主 docker-compose.yml 文件具有您在任何系统上 运行 容器集所需的设置,可以访问容器注册表。单独的 docker-compose.override.yml file 将支持开发人员在您拥有源代码副本的情况下使用。如果您使用 Compose 进行部署,则只需将主 docker-compose.yml 文件复制到目标系统。

# docker-compose.yml
version: '3.8'
services:
  client:
    image: registry.example.com/project/frontend
    ports: [...]
    environment: [...]
    restart: always
    # volumes: [...]
  express:
    image: registry.example.com/project/express
    ports: [...]
    environment: [...]
    restart: always
# docker-compose.override.yml
version: '3.8'
services:
  client:
    build: frontend
    # all other settings come from main docker-compose.yml
  express:
    build: express
    # all other settings come from main docker-compose.yml