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

I’m going to show you a live Playbook 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

  • install packages => ansible.builtin.apt
  • docker py module => ansible.builtin.pip
  • pull image => community.docker.docker_image
  • document root => ansible.builtin.file
  • custom index.html => ansible.builtin.copy
  • run container => community.docker.docker_container

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.

Join 50+ hours of courses in our exclusive community

Playbook

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

Conclusion

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, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

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

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza