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...

Building Docker Image with Maven

 Building Docker Image with Maven You are a DevOps Engineer working for ABC Infotech. A client has requested you to build a Docker image using Maven plugin, so that when you package your Maven project. Docker image will be built, and the generated artifacts will be deployed in local git repository  1. Create a Maven java project using CLI with the following configurations - groupId=javaApp  - artifactId maven-docker-image  2. Replace the 'Hello World!' message in the 'App.java' file with 'Hurray! You have successfully built a docker image using Maven' 3. Configure your 'pom.xml' to deploy the generated artifacts in the local git repository to build a docker image 4. The project should be deployed in the local repositary with the following details: - id = maven.repo - name = Maven Artifacts - URL = file:///home/labuser/Desktop/Remote/mavenArtifacts 5. The name of the jar file, repository name af the image should be same as the project artifactId...