简体   繁体   English

使用ansible管理docker容器 - 最佳实践

[英]Managing docker containers with ansible - best practices

I'm considering using both docker and ansible. 我正在考虑使用docker和ansible。 The idea I had was to use ansible to set up my instances and I was wondering what would be the best practice to do so: 我的想法是使用ansible来设置我的实例,我想知道这样做的最佳做法是什么:

  1. Calling ansible from the dockerfile on every container (which would necessitate having ansible installed on every container/instance. This method is mentioned in the ansible up and running book , on the docker episode); 从每个容器上的dockerfile调用ansible(这需要在每个容器/实例上安装ansible。这个方法在anser up and running book中 ,在docker剧集中提到); or 要么
  2. Running my containers and then setting up all the instances by executing ansible-playbook. 运行我的容器,然后通过执行ansible-playbook设置所有实例。

What would be the best approach? 什么是最好的方法? Are there any other alternative ways for such use case? 这种用例还有其他替代方法吗?

To use Ansible to setup your docker hosts (ie instances), you don't need to install Ansible on the remote machine. 要使用Ansible设置docker主机(即实例),您无需在远程计算机上安装Ansible。 You install Ansible on your primary machine, and run playbooks and ad-hoc commands from there. 您在主计算机上安装Ansible,并从那里运行playbooks和ad-hoc命令。 This is why Ansible is a good tool for this kind of task (ie installing things on remote machines). 这就是为什么Ansible是这类任务的好工具(即在远程机器上安装东西)。

For example, if your remote docker host is a CentOS 7 machine, you could use the following playbook to install docker based on the Docker install directions 例如,如果远程docker主机是CentOS 7计算机,则可以使用以下playbook根据Docker安装说明安装docker

- name: Install Docker on remote hosts
  hosts: docker-hosts
  sudo: yes
  tasks:
    - name: Install docker
      shell: curl -sSL https://get.docker.com | sh

Note that the docker-hosts group is defined by your hosts/inventory file. 请注意, docker-hosts组由hosts / inventory文件定义。

Once you have docker installed on the remote machines, you can create another Ansible playbook to create/run your containers. 一旦在远程计算机上安装了docker,就可以创建另一个Ansible playbook来创建/运行容器。

We commonly use the Ansible shell module in lieu of the docker module. 我们通常使用的Ansible shell模块代替的docker模块。 This is more for convenience and reference. 这更方便和参考。 So later on, someone can look at the shell command you use to deploy containers remotely as an example for their own development (ie "How do you run that 'docker run' command again?") 所以稍后,有人可以查看用于远程部署容器的shell命令,作为他们自己开发的示例(即“你如何再次运行'docker run'命令?”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM