运行 local ansible 创建本地 docker 图片
Running local ansible to create local docker Image
目录结构
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 DockerFile
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-compose
允许在 yml 中自定义构建上下文的配置。
这是我的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
我的问题:
- 我该如何解决这个问题?无需重新排列并且不更改现有的 DockerFile
- 如何将 ansible docker 模块配置到自定义上下文?
- 为什么 ansible docker 模块没有抛出错误?
回购回购:https://github.com/sathishsoundharajan/ansible-repro
它具有在任何本地人中重现该问题所需的文件
已通过以下更改修复
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 }}"
目录结构
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 DockerFile
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-compose
允许在 yml 中自定义构建上下文的配置。
这是我的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
我的问题:
- 我该如何解决这个问题?无需重新排列并且不更改现有的 DockerFile
- 如何将 ansible docker 模块配置到自定义上下文?
- 为什么 ansible docker 模块没有抛出错误?
回购回购:https://github.com/sathishsoundharajan/ansible-repro
它具有在任何本地人中重现该问题所需的文件
已通过以下更改修复
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 }}"