首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行本地接口创建本地码头映像

运行本地接口创建本地码头映像
EN

Stack Overflow用户
提问于 2021-09-04 16:58:07
回答 1查看 1.3K关注 0票数 2

目录结构

代码语言:javascript
复制
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来创建码头形象。这是我的剧本

代码语言:javascript
复制
- 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

代码语言:javascript
复制
FROM postgres:10.17-buster
COPY ../deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/

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

代码语言:javascript
复制
[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,我根本没有看到那个图像。

代码语言:javascript
复制
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供参考

代码语言:javascript
复制
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. How以将ansible对接模块配置为自定义上下文?
  3. 为什么不能抛出错误?

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

它有在任何本地用户中复制问题所需的文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-10 15:45:13

通过做下面的更改来修正

postgres/Dockerfile

代码语言:javascript
复制
FROM postgres:10.17-buster
COPY deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/

Ansible Playbook

代码语言:javascript
复制
- 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 }}"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69057345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档