Docker + Docker Compose + Ansible
-----------------------------------------------------------------------------------------------------------------------------
Flask Application Deployment using Ansible roles and Docker-Compose
1. Run the "setup.sh" file given in the path to install the required dependencies before starting the challenge.
2. A Flask application named "app.py" is given in the path.
3. Create an Ansible role named "Installation" in the path "/etc/ansible/roles" to install docker-compose.
4. Write an Ansible playbook named "creation.yaml" in the path , to perform the following tasks:
1. Using "file" and "copy" modules, create a Multi-stage "Dockerfile" in the path to
- Dockerize the given Flask application with "python:alpine3.7" as its base image, using the given 'requirements.txt' file.
- Build an image using "postgres" as its base image.
2. Using "file" and "copy" modules, create a "docker-compose.yaml" file in the path which creates two services named
- The "flask" service should run an "flask_image" image as a container named "flask_container" and expose it to port 5000.
- The "db" service should run an "postgres_image" image as a container named "postgres_container" and expose it to port 5476.
5. Write another Ansible playbook named "execute.yaml" in the path, to perform the following tasks using appropriate Dockerfile
- Import the "creation.yaml" playbook.
- Include the created "installation" role.
- Build a Docker image named "flask_image" using the created Dockerfile.
- Build a Docker image named "postgres_image" using the created Dockerfile.
- Run the "docker-compose.yaml" file.
6. Finally, run the "execute.yaml" playbook and check whether the Flask application is running in the specified port 5000.
-----------------------------------------------------------------------------------------------------------------------------
SOLUTION
1. create the Ansible role
mkdir etc/ansible/roles
cd etc/ansible/roles
ansible-galaxy role init Installation
2. create the creation.yaml, to create Dockerfile and docker-compose.yaml file
---
- name: Create Dockerfile and docker-compose files
hosts: localhost
tasks:
- name: Create Dockerfile
copy:
dest: ./Dockerfile
content: |
FROM python:alpine3.7 AS flask_img
WORKDIR /app
COPY requirements.txt /app
RUN pip install -r requirements.txt
COPY app.py .
EXPOSE 5000
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host", "0.0.0.0"]
# ------------------------------------
# ------------------------------------
FROM postgres AS postgres_img
ENV POSTGRES_DB myDB
ENV POSTGRES_USER myuser
ENV POSTGRES_PASSWORD mypassword
- name: Create docker-compose
copy:
dest: ./docker-compose.yaml
content: |
version: '3.4'
services:
flask:
image: flask_img
container_name: flask_cont
ports:
- 5000:5000
db:
image: postgres_img
container_name: postgres_cont
ports:
- 5476:5476
3. create the execute.yaml file
---
- name: Import creation.yml playbook
import_playbook: creation.yaml
- name: Execute all tasks
hosts: localhost
tasks:
- name: Include created role
include_role:
name: etc/ansible/roles/Installation
- name: Build flask_img using Dockerfile
command: docker build --target flask_img -t flask_img .
become: true
- name: Build postgress_img using Dockerfile
command: docker build --target postgres_img -t postgres_img .
become: true
- name: Build docker-compose file using Dockerfile
command: docker-compose up -d
# execute the playbooks using
# this is optional as this will create the Dockerfile and docker-compose file
ansible-playbook creation.yaml
# execute this playbook mainly
ansible-playbook execute.yaml
# check the url for the flask application
curl http://localhost:5000
Comments
Post a Comment