目录结构
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来创建码头形象。这是我的剧本
- 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 }}"我的邮政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 images,我根本没有看到那个图像。
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry 2 b2cb11db9d3d 3 days ago 26.2MB我的分析到现在为止:
我的DockerFile必须从项目的根路径复制一些脚本,这就是它拥有“../script/db-schema”的原因。我不能更改这个路径,因为构建中的docker-compose.dev.yml文件直接使用相同的docker-compose.dev.yml来创建dev容器。
我猜码头映像创建失败了,但是ansible模块不会抛出错误并提供成功的输出。
其次,ansible模块没有配置来像yml中docker-compose所允许的那样定制构建上下文。
这是我的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我的问题:
Repro回购:https://github.com/sathishsoundharajan/ansible-repro
它有在任何本地用户中复制问题所需的文件。
发布于 2021-11-10 15:45:13
通过做下面的更改来修正
postgres/Dockerfile
FROM postgres:10.17-buster
COPY deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/Ansible Playbook
- 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 }}"https://stackoverflow.com/questions/69057345
复制相似问题