2

目录结构

build
├── docker-compose.dev.yml
├── playbooks
│   ├── deploy-deps.yml
│   └── setup-local-registry.yml
├── postgres
│   └── DockerFile
└── rabbitmq
    ├── DockerFile
    └── init.sh
deploy-scripts
├── db-schema
│   └── global
│       ├── 01-init.sql
│       ├── 02-tables.sql
│       ├── 03-grant.sql
│       └── 04-index.sql
├── rabbitmq
│   └── global
│       └── prepare-queues.sh

我正在尝试使用 ansible 创建 docker 映像。这是我的剧本 yml

- hosts: localhost
  vars:
    registry_host: "localhost"
    registry_port: 5000
    i_postgres_image: "orderpostgres"
  tasks:
    - name: "Create & Push Postgres Image"
      docker_image:
        name: "{{ i_postgres_image  }}"
        build:
          path: "../../build/postgres"
        source: build
        state: present
      register: "out"
    - name: Show test output
      debug:
        msg: "{{ out }}"

我的 Postgres Docker 文件

FROM postgres:10.17-buster
COPY ../deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/

当我从项目根目录执行 ansible-playbook 时,这是执行的结果。

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ***********************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Create & Push Postgres Image] ****************************************************************************************************************************************************************************
changed: [localhost]

TASK [Show test output] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "actions": [
            "Built image orderpostgres:latest from ../../build/postgres"
        ],
        "changed": true,
        "failed": false,
        "image": null,
        "stdout": "",
        "stdout_lines": []
    }
}

PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

根据输出,我希望构建 docker 映像并准备好推送到本地注册表。但是当我执行时,docker images我根本看不到那个图像。

docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
registry     2         b2cb11db9d3d   3 days ago   26.2MB

到目前为止我的分析:

我的 DockerFile 必须从项目的根路径复制一些脚本,这就是它具有“../scripts/db-schema”的原因。我无法更改此路径,因为我的docker-compose.dev.yml文件在构建中直接使用相同的 DockerFile 来创建开发容器。

我猜 docker 镜像创建失败,但是 ansible 模块不会抛出错误并提供成功的输出。

其次,ansible docker 模块没有像docker-composeyml 中允许的那样自定义构建上下文的配置。

这是我docker-compose.dev.yml的参考

version: '3'
services:
    
  redis:
    image: redis:5.0
    container_name: 'cache'
    ports: 
      - 6379:6379
    volumes: 
      - ~/.docker-volume/redis/data/:/var/lib/redis

  postgres:
    build:
      context: ../
      dockerfile: ./build/postgres/DockerFile
    container_name: 'database'
    restart: always
    environment:
      - POSTGRES_USER=haha
      - POSTGRES_PASSWORD=haha
      - POSTGRES_DB=haha
    logging:
      options: 
        max-size: 10m
        max-file: '3'
    ports: 
      - 5432:5432
    volumes: 
      - ~/.docker-volume/postgres/data/:/var/lib/postgresql/data

我的问题:

  1. 我怎么解决这个问题 ?无需重新安排,也无需更改现有的 DockerFile
  2. 如何将 ansible docker 模块配置到自定义上下文?
  3. 为什么 ansible docker 模块没有抛出错误?

Repro 回购:https ://github.com/sathishsoundharajan/ansible-repro

它具有在本地任何人中重现问题所需的文件

4

1 回答 1

1

通过进行以下更改修复

postgres/Dockerfile

FROM postgres:10.17-buster
COPY deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/

Ansible 剧本

- hosts: localhost
  vars:
    registry_host: "localhost"
    registry_port: 5000
    i_postgres_image: "order-postgres"
  tasks:
    - name: "Pull & Initialize Postgres Image"
      docker_image:
        name: "{{ i_postgres_image  }}"
        build:
          path: ../../
          dockerfile: build/postgres/Dockerfile
        source: build
        state: present

    - name: "Tag & Push to registry"
      register: "out"
      docker_image:
        name: "{{ i_postgres_image }}"
        repository: "{{ registry_host }}:{{ registry_port }}/{{ i_postgres_image }}"
        push: yes
        source: local

    - name: Show test output
      debug:
        msg: "{{ out }}"
于 2021-11-10T15:45:13.063 回答