使用 Ansible 在 Docker 容器内运行命令 [英] Run Command Inside of Docker Container Using Ansible

查看:55
本文介绍了使用 Ansible 在 Docker 容器内运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完成的是在已经使用 Ansible 在 Digital Ocean Ubuntu/Docker Droplet 上创建的 Docker 容器内运行命令.

what I'm trying to accomplish is to run commands inside of a Docker container that has already been created on a Digital Ocean Ubuntu/Docker Droplet using Ansible.

似乎找不到任何关于此的信息,或者我主要遗漏了一些东西.这是我在剧本中的 Ansible 任务.我对 Ansible 很陌生,因此非常感谢任何建议或智慧.

Can't seem to find anything on this, or I'm majorly missing something. This is my Ansible task in my play book. I'm very new to Ansible so any advice or wisdom would be greatly appreciated.

- name: Test Deploy
    hosts: [my-cluster-of-servers]

tasks: 
  - name: Go Into Docker Container And Run Multiple Commands
    docker:
      name: [container-name]
      image: [image-ive-created-container-with-on-server]
      state: present
      command: docker exec -it [container-name] bash

推荐答案

您应该能够使用 docker exec:

You should be able to execute a script (with your sequence of command in it) with docker exec:

docker exec container-name bash -l -c /path/to/script > /path/to/log

(另请参阅为什么我必须在容器内使用 bash -l -c?")

(See also "Why do I have to use bash -l -c inside my container?")

  • /path/to/script 应该可以被您的 Ansible 进程访问.
  • /path/to/log 是容器内的路径,可以在卷中共享.
  • /path/to/script should be accessible by your Ansible process.
  • /path/to/log is a path inside the container, that could be shared in a volume.

这篇关于使用 Ansible 在 Docker 容器内运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆