部署包含 docker-compose.yml 的 Ansible 项目 [英] Deploy Ansible project which include a docker-compose.yml

查看:51
本文介绍了部署包含 docker-compose.yml 的 Ansible 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Ansible 来部署我的一个项目(我们称之为 project-to-deploy).

I woud like to use Ansible to deploy one of my project (let's call it project-to-deploy).

project-to-deploy 可以使用 docker-compose.yml 文件在本地运行,该文件除其他外,在 中安装以下卷码头集装箱.

project-to-deploy can be run locally using a docker-compose.yml file, which, among other things, mount the following volumes inside the docker-container.

version: "2"
services:
  database:
    image: mysql:5.6
      volumes:
        - ./docker/mysql.init.d:/docker-entrypoint-initdb.d
  messages:
      image: private.repo/project-to-deploy:latest

这里没有什么更有用的了.运行项目:docker-compose up.我已经创建了项目的docker image(其中我将项目中的所有文件复制到新创建的docker image),并将其上传到private.repo/project-to-deploy:latest.

Nothing more useful here. To run the project: docker-compose up. I have created a docker image of the project (in which I copy all the files from the project to the newly created docker image), and uploaded it to private.repo/project-to-deploy:latest.

现在是 Ansible 部分.
要运行该项目,我需要:

Now comes the Ansible part.
For the project to run, I need:

  1. 泊坞窗图像
  2. 一个 MySQL 实例(见下面我的 docker-compose.yml 部分)

在我的docker-compose.yml(上面)中,这样做很容易.我只是创建了 2 个服务(数据库和 project-to-deploy)并将它们相互链接.

如何在 Ansible 中执行此类操作?

In my docker-compose.yml (above), it is quite easy to do so. I just create the 2 services (database and project-to-deploy) and link them each-other.

How can I perform such action in Ansible?

我做的第一件事是获取图像:

First things I did is to fetch the image:

- name: Docker - pull project image
  docker:
    image: "private.repo/project-to-deploy:latest"
    state: restarted
    pull: always

那么,我怎样才能将 MySQL docker 镜像链接到这个,知道MySQL docker 镜像需要 project-to-deploy 的文件?

Then, how can I link the MySQL docker image to this, knowing that the MySQL docker image need files from project-to-deploy ?

如果您想到其他方法,请随时提出建议!

If you think of another way to do it, feel free to make suggestions !

推荐答案

轻微更正,docker 模块用于运行容器,在您的示例中,您不仅仅是获取图像.您实际上是在拉取映像、创建容器并运行它.

slight correction, the docker module is for running containers, in your example you are not just fetching the image. You're actually pulling the image, creating a container, and running it.

我通常会通过使用 ansible 将每个容器的配置文件模板化为模板,其中包含所需的 IP 地址、端口、凭据等来实现这一点,为它们提供相互通信所需的一切.

I would typically accomplish this by using ansible to template each container's config files with the needed IP addresses, ports, credentials, etc. providing them all they need to know to communicate with each other.

由于您的示例只涉及很少的连接,您可以在您的 ansible 任务中设置 links 选项.您应该只需要在消息"容器端设置它.

Since your example only involves few connections you could set the links option in your ansible task. You should only need to set it on the "messages" container side.

- name: Docker - start MySQL container
  docker:
    name: database
    image: "mysql:5.6"
    state: restarted
    volumes:
    - /path/to/docker/mysql.init.d:/docker-entrypoint-initdb.d
    pull: always

- name: Docker - start project container
  docker:
    name: messages
    image: "private.repo/project-to-deploy:latest"
    state: restarted
    pull: always
    links:
    - database

这篇关于部署包含 docker-compose.yml 的 Ansible 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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