You can deploy MariaDB in a Docker container using Ansible.
For this section you will need a computer which has Ansible installed. You can use the same SSH key pair. You also need a cloud instance or VM, or a physical machine with Ubuntu installed, running and ready to deploy MariaDB.
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. If you are new to Docker, consider reviewing Learn how to use Docker .
To run Ansible, you can use an Ansible playbook. The playbook uses the community.docker
collection to deploy MariaDB in a container.
The playbook maps the container port to the host port, which is 3306
.
playbook.yml
.
---
- hosts: all
remote_user: root
become: true
tasks:
- name: Update the Machine and Install dependencies
shell: |
apt-get update -y
apt-get -y install mariadb-client
apt-get install docker.io -y
usermod -aG docker ubuntu
apt-get -y install python3-pip
pip3 install PyMySQL
pip3 install docker
become: true
- name: Reset ssh connection for changes to take effect
meta: "reset_connection"
- name: Log into DockerHub
community.docker.docker_login:
username: {{dockerhub_uname}}
password: {{dockerhub_pass}}
- name: Deploy mariadb docker container
docker_container:
image: mariadb:latest
name: mariadb_test
state: started
ports:
- "3306:3306"
pull: true
volumes:
- "db_data:/var/lib/mysql:rw"
- "mariadb-socket:/var/run/mysqld:rw"
- "/tmp:/tmp:rw"
restart: true
env:
MARIADB_ROOT_PASSWORD: {{your_mariadb_password}}
MARIADB_USER: local_us
MARIADB_PASSWORD: Armtest123
MARIADB_DATABASE: arm_test
playbook.yml
to use your valuesReplace {{your_mariadb_password}} with your own password.
Also, replace {{dockerhub_uname}} and {{dockerhub_pass}} with your Docker Hub credentials.
inventory.txt
file and copy the contents given below:
[all]
ansible-target1 ansible_connection=ssh ansible_host={{public_ip of VM where MariaDB to be deployed}} ansible_user={{user_of VM where MariaDB to be deployed}}
inventory.txt
to use your valuesReplace {{public_ip of VM where MariaDB to be deployed}} and {{user_of VM where MariaDB to be deployed}} with your own values.
ansible-playbook
command:
ansible-playbook playbook.yaml -i {your_inventory_file_location}
yes
when prompted for the SSH connection.Deployment may take a few minutes.
The output should be similar to:
PLAY [all] *****************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************
The authenticity of host '3.19.242.129 (3.19.242.129)' can't be established.
ED25519 key fingerprint is SHA256:uWZgVeACoIxRDQ9TrqbpnjUz14x57jTca6iASH3gU7M.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
ok: [ansible-target1]
TASK [Update the Machine and install docker dependencies] *************************************************************************************************************
changed: [ansible-target1]
TASK [Reset ssh connection for changes to take effect] ****************************************************************************************************************
TASK [Log into DockerHub] *********************************************************************************************************************************************
changed: [ansible-target1]
TASK [Deploy mariadb docker containere] *******************************************************************************************************************************
changed: [ansible-target1]
PLAY RECAP ************************************************************************************************************************************************************
ansible-target1 : ok=3 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You can use the instructions from the previous topic to connect to the database and confirm the Docker container deployment is working.