首页 > 解决方案 > 如何在 ansible 中编写任务,以便它们执行与 docker build 和 docker run 相同的操作

问题描述

我搜索了论坛,但找不到任何东西。如果是这样,请戳你的手指)如何在 ansible 中编写任务,以便它们执行相同的操作:

docker build. -t alpine: volume
docker run --rm -ti -v colibri:/colibri alpine: volume

这将为我创建一个 docker 映像并连接该卷,以便文件在那里同步。我的 Dockerfile 看起来像这样:

FROM alpine:3.12

RUN apk add unzip && \
    addgroup -S -g 9999 www && \
    adduser -u 9999 -S -G www www && \
    mkdir /colibri && chown www:www /colibri

COPY artifact.zip /colibri/artifact.zip
USER www
WORKDIR colibri
RUN unzip artifact.zip && rm artifact.zip

ansible中的任务:

- name: Build image
  community.docker.docker_image:
    build:
      path: "{{ remote_path }}/docker/volume"
    name: volume
    tag: v1
    push: no
    source: build
    
  - name: Build an volume on artefact
  community.docker.docker_container:
    name: volume:v1
    state: present
    volumes:
      - colibri:/colibri
    cleanup: yes


   

标签: dockeransible

解决方案


我会自己回答我自己的问题。通过测试和错误,我得出了这个结果

- name: Build an image
  community.docker.docker_image:
    build:
      path: "{{ remote_path }}/docker/volume"
    name: volume
    tag: v1
    source: build

- name: Build an artefact on volume
  community.docker.docker_container:
    name: volume
    image: volume:v1
    state: started
    timeout: 300
    volumes:
      - colibri_magento:/colibri
    auto_remove: yes
    cleanup: yes

推荐阅读