【问题标题】:Copy file to docker container via Ansible通过 Ansible 将文件复制到 docker 容器
【发布时间】:2022-12-28 14:23:51
【问题描述】:

作为我的 Ansible 剧本步骤之一,我想将文件复制到 docker 容器。我使用 jinja2“模板”创建文件。我可以复制 /tmp/ 中的文件并运行命令将其复制到 docker 容器中,例如:
`docker cp /tmp/config.json my_image:/app/config/path/`
但我正在寻找不使用“/tmp”之类的更好方法。

【问题讨论】:

    标签: docker ansible docker-cp


    【解决方案1】:

    Ansible 有一个 docker connection plugin,您可以使用它与您的剧本中的现有容器进行交互。例如,如果我有一个名为 mycontainer 的容器:

    $ docker ps
    CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
    07899303ac55   alpine    "sleep inf"   7 seconds ago   Up 2 seconds             mycontainer
    

    我可以像这样创建一个 Ansible 清单,将 ansible_connection 变量设置为 community.general.docker

    all:
      hosts:
        mycontainer:
          ansible_connection: community.docker.docker    
    

    现在我可以像这样在游戏中定位容器:

    - hosts: mycontainer
      gather_facts: false
      become: true
      tasks:
        - name: create target directory in container
          file:
            path: /target
            state: directory
    
        - name: copy a file into the container
          copy:
            src: example.file
            dest: /target/example.file
    

    【讨论】:

      【解决方案2】:

      如果我的容器存在于远程主机中,我该如何定义主机文件?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多