如何使用 Ansible 的 docker_image 和 docker_container 模块在类似 Debian 的系统中自动化部署一个独立运行(无需登录用户)的 Apache httpd Web Server 容器,并将端口 80 映射到 8080,同时设置自定义的 Web 根目录。

部署 Apache Web Server 的 Docker 容器

概述

部署 Apache Web Server 的 Docker 容器需要以下步骤:

  1. 安装必要的 Python 包和依赖项。
  2. 安装 Docker 的 Python 模块。
  3. 从 Docker Hub 拉取 Apache httpd 的 Docker 镜像。
  4. 创建 Web 根目录并设置正确的权限。
  5. 创建自定义的 index.html 文件。
  6. 启动 Web Server 容器,设置正确的端口和卷挂载。

Ansible 模块

1. ansible.builtin.apt

安装必要的系统包,如 python3-pip 和 virtualenv。

2. ansible.builtin.pip

安装 Docker 的 Python 模块。

3. community.docker.docker_image

从 Docker Hub 拉取 Apache httpd 的镜像。

4. ansible.builtin.file

创建 Web 根目录。

5. ansible.builtin.copy

创建自定义的 index.html 文件。

6. community.docker.docker_container

运行 Web Server 容器,设置端口映射和卷挂载。

示例代码

以下是一个完整的 Ansible Playbook,用于在类似 Debian 的系统中部署 Apache Web Server 的 Docker 容器:

---
- name: deploy httpd on container
  hosts: all
  become: true
  gather_facts: false
  vars:
    webroot: "/webroot/"
  tasks:
    - name: system packages present
      ansible.builtin.apt:
        name:
          - python3-pip
          - virtualenv
          - python3-setuptools
        state: latest
        update_cache: true

    - name: Docker Module for Python
      ansible.builtin.pip:
        name: docker

    - name: pull image
      community.docker.docker_image:
        name: httpd
        source: pull
        tag: latest

    - name: webroot present
      ansible.builtin.file:
        path: "{{ webroot }}"
        state: directory

    - name: custom index.html
      ansible.builtin.copy:
        dest: "{{ webroot }}index.html"
        content: |
          Custom Web Page

    - name: run httpd container
      community.docker.docker_container:
        name: webserver
        image: httpd
        state: started
        detach: true
        exposed_ports:
          - 80
        ports:
          - 8080:80
        volumes: "{{ webroot }}:/usr/local/apache2/htdocs/"

执行过程

执行命令

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory container/docker_httpd_debian.yml

输出结果

PLAY [deploy httpd on container] ***********************************************
TASK [system packages present] *************************************************
changed: [ubuntu.example.com]
TASK [Docker Module for Python] ***********************************************
ok: [ubuntu.example.com]
TASK [pull image] **************************************************************
changed: [ubuntu.example.com]
TASK [webroot present] *********************************************************
changed: [ubuntu.example.com]
TASK [custom index.html] *******************************************************
changed: [ubuntu.example.com]
TASK [run httpd container] *****************************************************
changed: [ubuntu.example.com]
PLAY RECAP *********************************************************************
ubuntu.example.com         : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试

再次运行 Playbook 时,不会重复执行已成功完成的任务:

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory container/docker_httpd_debian.yml

输出结果:

PLAY [deploy httpd on container] ***********************************************
TASK [system packages present] *************************************************
ok: [ubuntu.example.com]
TASK [Docker Module for Python] ***********************************************
ok: [ubuntu.example.com]
TASK [pull image] **************************************************************
ok: [ubuntu.example.com]
TASK [webroot present] *********************************************************
ok: [ubuntu.example.com]
TASK [custom index.html] *******************************************************
ok: [ubuntu.example.com]
TASK [run httpd container] *****************************************************
ok: [ubuntu.example.com]
PLAY RECAP *********************************************************************
ubuntu.example.com         : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

在执行 Playbook 之前,目标主机上未安装 Apache httpd 的 Docker 镜像,也没有运行的容器:

$ docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2
$ sudo docker images ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

执行后

执行 Playbook 后,Apache httpd 的 Docker 容器已成功启动,并且可以通过端口 8080 访问:

ansible-pilot $ ssh devops@ubuntu.example.com
$ sudo docker images ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
httpd        latest    c477f001191f   5 minutes ago   123MB

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                  NAMES
c477f001191f   httpd     "httpd-foreground"   50 seconds ago   Up 49 seconds   0.0.0.0:8080->80/tcp   webserver

通过以上步骤,你可以使用 Ansible 在类似 Debian 的系统中自动化部署 Apache Web Server 的 Docker 容器,并确保其正常运行。