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

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

问题描述

我想使用 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本地运行文件,其中除了别的以外,将以下卷装载到 docker-container 中。

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.

现在是可复制的部分。

对于要运行的项目,我需要:

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


  1. 码头图像

  2. 一个MySQL实例(参见我的 docker-compose.yml 的一部分)

  1. The docker image
  2. A MySQL instance (see part of my docker-compose.yml below)

在我的 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图像需要文件从项目到部署

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.

由于您的示例只涉及到很少的连接,您可以设置链接选项在您的可执行任务。您只需要在消息容器端设置它。

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的可复制项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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