Skip to main content

Docker + Docker Compose + Ansible

 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

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command: ...

Mini-Projects for Docker, Docker Swarm

 Mini-Projects for Docker Challenge 1 Welcome to the Docker challenge, your task is to follow the below steps and complete them successfully. Step 1: Pull latest nginx image Step 2: Create a new bridge 'bridge_sample' Step 3: Run a couple of images (Cont1 and Cont2) and connect these to the new bridge created. Now try to ping from cont1 to cont2 to verify connectivity. Step 4: Stop containers Step 5: Remove network, containers, and images using docker commands Note: Execute "history -w" in the terminal before submitting the scenario. Before moving to Answers try to solve yourself... Let's get started... Step 1: Pull latest nginx image docker pull nginx:latest Step 2: Create a new bridge 'bridge_sample' docker network create bridge_sample Step 3: Run containers and connect them to the new bridge docker run -d --name Cont1 --network bridge_sample nginx:latest docker run -d --name Cont2 --netw...

Hands-on with Docker and Docker-Compose

  Hands-on with Docker & Docker-Compose (1) Create a "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose working-with-multiple-services/app", with base image "python:3.7" to dockerize the given Flask application "app.py" (2) Create another "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/db", with base image "postgres" and configure the environment variables postgres user "abc", postgres password "abc@123", postgres db "postgres". (3) Create one more "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/cache", with base image as "redis". (4) Create a "docker-compose.yml" file inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services" with the below specifications,      (4.1) Create three s...