首页 > 解决方案 > AWS 在 ec2 实例上的 docker 中启动示例网站

问题描述

我需要一些帮助在 docker 中使用 ansible 在 ec2 实例中部署一个简单的网站。目前,我有一个提供 ec2 实例的剧本,但我希望能够安装 docker 和 apache 服务器并部署一个简单的网站。例如,只是一个简单的网页打招呼,所以我知道它正在工作。我不知道该怎么做

- name: deploy Ec2 Instances
  hosts: localhost
  connection: local
  gather_facts: true

  tasks:
  - name: Launch ec2 instance
    ec2:
      instance_type: t2.micro
      key_name: mykeypair
      image: ami-00eb20669e0990cb4
      region: us-east-1
      group: default
      count: 1
      vpc_subnet_id: subnet-ed43648a
      wait: yes

标签: dockeramazon-ec2ansible

解决方案


以下非常简化的方法应该适合您的需求:

编辑(对于这个 apache 示例,不需要自定义 Dockerfile)!

您可以在 docker-compose.yml 文件中引用 apache(该文件必须位于相对于您的 playbook 文件的 files 或 templates 目录中):

version: '3.7'
services:
  apache:
    image: httpd:2.4-alpine
    ports:
      - "80:80"

将这些任务添加到您的游戏中:

# you probably have to create your Docker Directories first and check for existence of your files on the host

...

- name: "transfer dockerfiles for service"
  template:
    src: docker-compose.yml
    dest: "/home/ec2-user/deploy/apache/docker-compose.yml"
    mode: 0755
    trim_blocks: no

- name: "(re)start service"
  shell: |
    cd /home/ec2-user/deploy/apache
    docker-compose up --build -d

完成此 playbook 运行后,您可以简单地在主机本身上 curl localhost/index.html。


推荐阅读