Ansible Pilot

Deploy Apache Web Server in a Docker Container for Debian-like systems - Ansible modules docker_image and docker_container

How to automate the setup of a detached Apache httpd Web Server tag:latest in a Docker Container pass-throw of port 8080 to 80 with a custom webroot for Debian-like systems.

How to Setup Apache Web Server in a Docker Container for Debian-like systems with Ansible?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Setup Apache Web Server in a Docker Container for Debian-like systems

Today we’re talking about how to deploy a web server apache httpd in a Docker Container for Debian-like Linux systems. The full process requires six steps that you could automate with different Ansible modules. Firstly you need to install some python packages and dependencies using the ansible.builtin.apt Ansible module. Secondly, you need to install the Docker Module for Python using the ansible.builtin.pip Ansible module. Thirdly, you need to pull the image for the docker hub registry using the community.docker.docker_image Ansible module. Fourthly, you need to create the document root with the right permission with the ansible.builtin.file module. Fifty, you need to create the custom index.html with ansible.builtin.copy Ansible module. You could upgrade this step using the template module. Finally, you could run the webserver container setting the right port and volume settings using the community.docker.docker_container Ansible module.

demo

Setup Apache Web Server in a Docker Container for Debian-like systems with Ansible Playbook.

code

---
- 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/"

execution

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
ansible-pilot $

before execution

$ 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 containers ls
docker: 'containers' is not a docker command.
See 'docker --help'
$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$

after execution

ansible-pilot $ ssh [email protected]
$ sudo docker images ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
$ 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
$

Apache Web Server in a Docker Container for Debian-like systems

code with ❤️ in GitHub

Recap

Now you know how to set up Apache Web Server in a Docker Container for Debian-like systems with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

March 4, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress