SQLite is an embedded database that runs within the application process, so there is no dedicated SQLite server container. This playbook demonstrates how to deploy applications with SQLite in Docker containers using Ansible, with proper volume persistence.
---
- name: Deploy Application with SQLite in Docker
hosts: sqlite_docker
become: true
vars:
app_name: myapp
app_version: "latest"
app_base_dir: /opt/{{ app_name }}
app_data_dir: "{{ app_base_dir }}/data"
app_container_name: "{{ app_name }}-container"
app_host_port: 8000
app_bind_address: "0.0.0.0"
sqlite_journal_mode: "WAL"
tasks:
- name: Create base directories
file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- "{{ app_base_dir }}"
- "{{ app_data_dir }}"
- name: Install Docker packages
package:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
notify: restart docker
- name: Enable and start Docker service
systemd:
name: docker
state: started
enabled: true
- name: Create Docker Compose file
copy:
dest: "{{ app_base_dir }}/compose.yaml"
content: |
services:
{{ app_name }}:
image: {{ app_name }}:{{ app_version }}
container_name: {{ app_container_name }}
restart: unless-stopped
volumes:
- ./data:/data
environment:
- DATABASE_URL=/data/app.db
- SQLITE_JOURNAL_MODE={{ sqlite_journal_mode }}
ports:
- "{{ app_bind_address }}:{{ app_host_port }}:8000"
healthcheck:
test: ["CMD", "sqlite3", "/data/app.db", "SELECT 1;"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
volumes:
sqlite-data:
driver: local
mode: '0644'
notify: restart app container
- name: Start application container
community.docker.docker_compose_v2:
project_src: "{{ app_base_dir }}"
state: present
build: never
- name: Wait for application to become available
wait_for:
host: "{{ app_bind_address }}"
port: "{{ app_host_port }}"
delay: 2
timeout: 60
delegate_to: localhost
- name: Display status
debug:
msg: "Application with SQLite is running"
handlers:
- name: restart docker
systemd:
name: docker
state: restarted
- name: restart app container
community.docker.docker_compose_v2:
project_src: "{{ app_base_dir }}"
state: present
build: never
listen: restart app container
ansible-playbook -i inventory.yml sqlite-docker.yml