GNU/Linux >> Linux Esercitazione >  >> Panels >> Docker

Distribuisci WordPress su Docker usando Ansible

Panoramica

Il seguente articolo tratterà i passaggi su come distribuire WordPress su Docker utilizzando Ansible. Lo scopo di questa procedura è automatizzare il processo di distribuzione di WordPress su Docker con il playbook Ansible.

Per distribuire wordpress su Docker utilizzando ansible con successo, è necessario configurare prima alcuni requisiti (elencati di seguito nei prerequisiti)

Prerequisiti

  • Docker installato e in esecuzione sul computer host di destinazione (l'installazione di Docker può anche essere automatizzata con Ansible – collegamento al POST)
  • Accesso SSH abilitato sugli host remoti con i parametri di accesso preimpostati nel file Ansible hosts
  • Ansible installato sulla macchina client (la tua macchina)
  • Modulo Docker Python e Python installato per Ansible sul computer di destinazione
  • Installato Python sul tuo computer locale

Installa il modulo Docker Python per Ansible

La maggior parte delle distribuzioni Linux ha Python3 preinstallato, ma per altre potrebbe mancare il modulo Python Docker utilizzato da Ansible. Saprai che è se ricevi un errore che indica che il modulo è mancante o non trovato. Esempio di immagine dell'errore sotto:

Il modulo menzionato è in realtà l'SDK Docker che Python utilizza per lavorare con Docker. Il modo più semplice per installare il modulo Python Docker è con lo strumento "pip". Se manca lo strumento "pip", puoi installarlo facilmente e poi con esso installare il modulo docker python:

Debian/Ubuntu

sudo apt install python3-pip

Fedora

sudo dnf install python3-pip

CentOS/RedHat

sudo yum python3-pip

Dopo aver installato il pip, esegui il comando per installare il modulo docker:

pip3 install docker

Se per caso riscontri un errore in Ansible che non riesce a trovare il modulo Python, aggiungi una variabile dell'interprete Python nel tuo file hosts. Nella maggior parte dei casi si trova in "/usr/bin/python3""/usr/lib/python3" .

L'errore è simile a questo:

La variabile interprete nel file hosts è simile a questa:

ansible_python_interpreter=/usr/bin/python3

Annota gli host nel file hosts con i parametri di accesso

Primo passo:aggiungere i parametri necessari nel file hosts in modo che Ansible possa raggiungere, accedere e interagire con la nostra macchina:

sudo nano /etc/ansible/hosts

Nel file hosts, aggiungi i parametri per assomigliare a questo:

Dopo aver aggiunto i parametri necessari per il nostro host remoto, salva il file ed esci.

Playbook Ansible per l'implementazione di WordPress su Docker

Per questa distribuzione, utilizzeremo il seguente playbook:

---
- hosts: docker
    vars:
      db_volume: mariadb
      wordpress: wordpress
    tasks:
      - name: Deploy MariaDB server
        docker_container:
          image: mariadb
          name: mariadb
          volumes:
            - "{{db_volume}}:/var/lib/mysql"
          env:
            MYSQL_ROOT_PASSWORD: somerootpassword
            MYSQL_PASSWORD: somemysqlpassword
            MYSQL_DATABASE: db
            MYSQL_USER: mysqluser

      - name: Deploy WordPress
        docker_container:
          image: wordpress
          name: wordpress
          restart_policy: always
          ports:
            - "8080:80"
          links:
            - "{{db_volume}}:/var/lib/mysql"
          volumes:
            - "{{wordpress}}:/var/www/html"
          env:
            MYSQL_PASSWORD: somemysqlpassword
            MYSQL_DATABASE: db
            MYSQL_USER: mysqluser
            MYSQL_HOST: mariadb

Sentiti libero di copiarlo.

Ripartizione del Playbook:

hosts: docker // variable to target only machine hosts that are in the docker group
vars: 

db_volume: mariadb 

wordpress: wordpress  // [OPTIONAL] defined variables for each container. These are used for setting volumes on the host and are matching the container names.
tasks: // Defined a task which will deploy a MariaDB container(MariaDB database server in container form). Task will pull down the official Docker image of MariaDB from the Docker hub, set a name container name "mariadb" and set a persistent volume on the host machine for the database storage.

- name: Deploy MariaDB server // Task name

docker_container: // Docker function that Ansible will use

image: mariadb  // Docker image to pull down

name: mariadb // Specify the container name

volumes: - "{{db_volume}}:/var/lib/mysql" // Specify a volume on the host machine for persistent storage
env: // Environment variables to define parameters for the database such as the root password, admin user password, name of the database and the user name of the new user on the MariaDB server

MYSQL_ROOT_PASSWORD: somerootpassword // MySQL root password

MYSQL_PASSWORD: somemysqlpassword // MySQL admin/standard user password to be used by WordPress

MYSQL_DATABASE: db // MySQL database name

MYSQL_USER: mysqluser // Admin/standard user username for WordPress to use
// This the task that will deploy the WordPress Docker container. Same just like for the MariaDB container, Ansible will pull down the official WordPress image from the Docker hub. Here we also specified the container restart policy(when to restart the container) and also set number of ports to expose on the container and bind to the host, so that the container can be accessible via browser and http protocol.

- name: Deploy WordPress // Task name

docker_container: // Docker function that Ansible will use

image: wordpress // Docker image to pull down

name: wordpress // Specify the container name

restart_policy: always // Set attribute for container restart

ports: - "8080:80" // Specify ports to expose on the container to be accessible via web browser

links:
- "{{db_volume}}:/var/lib/mysql // Variable to specify the link to the MySQL server so that WordPress can connect to the database

volumes: - "{{wordpress}}:/var/www/html" // Specify a volume on the host machine for persistent storage
// Environment variables for the MariaDB database, for WordPress to use in order to connect to the database and use the database for data storage.
env: 

MYSQL_PASSWORD: somemysqlpassword // Variable to specify MySQL for WordPress to use

MYSQL_DATABASE: db // MySQL database name which will WordPress connect to 

MYSQL_USER: mysqluser // MySQL user for WordPress to use

MYSQL_HOST: mariadb // MySQL database server to connect to(docker container name we previously set)

Distribuisci WordPress su Docker utilizzando Ansible

Una volta che abbiamo il nostro playbook Ansible, esegui il playbook:

docker deploy-wordpress.yml -l docker

Risultati attesi:

Verifica se i container sono in esecuzione e che WordPress sia accessibile tramite browser:

Riepilogo

Per riassumere l'articolo, siamo riusciti a distribuire con successo WordPress su Docker utilizzando Ansible e con ciò abbiamo automatizzato il processo di distribuzione di WordPress su Docker. Sebbene l'ambiente Ansible fosse necessario per la configurazione con il modulo Python e Python Docker affinché questo processo avesse successo. Successivamente abbiamo scritto ed eseguito il playbook Ansible che distribuisce WordPress con il database e ha anche la persistenza dei dati in modo che i dati e i file non siano archiviati nel contenitore Docker.

Grazie per il tuo tempo...


Docker
  1. Macchine virtuali multipass utilizzando Ansible

  2. Configura Nextcloud con Redis usando Docker

  3. Distribuisci WordPress su Docker usando Ansible

  4. Distribuisci Nextcloud su Docker utilizzando Ansible

  5. Come distribuire PostgreSQL come contenitore Docker

Come installare Ansible su Ubuntu 20.04

Come distribuire Pi-Hole su Debian 11

Installa Navidrome usando Docker

Come installare Gitea su Ubuntu usando Docker

Distribuire un'installazione di MariaDB di Production Docker

Utilizzo di Docker Desktop per gestire un container