This guide provides a full Ansible playbook to deploy Apache Tomcat using Docker Compose.
It covers Debian 10 to latest stable, Ubuntu LTS releases, and RHEL 9+ compatible systems.
- name: Install Apache Tomcat with Docker
hosts: apache-tomcat
become: true
vars:
tomcat_project_dir: /opt/apache-tomcat
tomcat_version: "11.0"
tomcat_image: "tomcat:{{ tomcat_version }}-jdk17-temurin"
tomcat_container_name: apache-tomcat
tomcat_host_port: 8080
tomcat_container_port: 8080
tomcat_config_dir: "{{ tomcat_project_dir }}/config"
tomcat_webapps_dir: "{{ tomcat_project_dir }}/webapps"
tomcat_logs_dir: "{{ tomcat_project_dir }}/logs"
tasks:
- name: Install Docker prerequisites on Debian/Ubuntu
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install Docker prerequisites on RHEL family
dnf:
name:
- dnf-utils
state: present
when: ansible_os_family == "RedHat"
- name: Add Docker GPG key on Debian/Ubuntu
apt_key:
url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
state: present
when: ansible_os_family == "Debian"
- name: Add Docker repository on Debian/Ubuntu
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
state: present
when: ansible_os_family == "Debian"
- name: Add Docker repository on RHEL family
yum_repository:
name: docker-ce
description: Docker CE Repository
baseurl: "https://download.docker.com/linux/centos/$releasever/$basearch/stable"
gpgcheck: yes
gpgkey: https://download.docker.com/linux/centos/gpg
when: ansible_os_family == "RedHat"
- name: Install Docker packages on Debian/Ubuntu
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install Docker packages on RHEL family
dnf:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
when: ansible_os_family == "RedHat"
- name: Enable and start Docker service
systemd:
name: docker
state: started
enabled: true
- name: Create Tomcat project directory
file:
path: "{{ tomcat_project_dir }}"
state: directory
mode: "0755"
- name: Create Tomcat config directory
file:
path: "{{ tomcat_config_dir }}"
state: directory
mode: "0755"
- name: Create Tomcat webapps directory
file:
path: "{{ tomcat_webapps_dir }}"
state: directory
mode: "0755"
- name: Create Tomcat logs directory
file:
path: "{{ tomcat_logs_dir }}"
state: directory
mode: "0755"
- name: Create Docker Compose file
copy:
dest: "{{ tomcat_project_dir }}/compose.yml"
mode: "0644"
content: |
services:
apache-tomcat:
image: {{ tomcat_image }}
container_name: {{ tomcat_container_name }}
ports:
- "{{ tomcat_host_port }}:{{ tomcat_container_port }}"
volumes:
- {{ tomcat_config_dir }}:/usr/local/tomcat/conf
- {{ tomcat_webapps_dir }}:/usr/local/tomcat/webapps
- {{ tomcat_logs_dir }}:/usr/local/tomcat/logs
restart: unless-stopped
environment:
- TZ=UTC
- name: Start Apache Tomcat container
community.docker.docker_compose_v2:
project_src: "{{ tomcat_project_dir }}"
state: present
- name: Verify container is running
community.docker.docker_container_info:
name: "{{ tomcat_container_name }}"
register: tomcat_container_info
retries: 5
delay: 3
until: tomcat_container_info.exists == true
latest.docker compose command).After deployment, access Tomcat at:
http://SERVER_IP:8080
Copy your WAR files to the webapps directory:
sudo cp your-app.war /opt/apache-tomcat/webapps/
docker logs apache-tomcat
# or
tail -f /opt/apache-tomcat/logs/catalina.out
cd /opt/apache-tomcat
docker compose stop
docker compose start
Any questions?
Feel free to contact us. Find all contact information on our contact page.