Skip to main content

Build Flask image using docker & docker-compose

 Build Flask Image using docker & docker-compose




1. Create a Dockerfile to build Flask image with Flask application, 'requirements.txt' and expose it in port 5000.
2. Create a 'docker-compose.json' file to create two services named 'python_web_app' and 'db_service'
3. The 'python_web_app' service  should be built from Dockerfile and specify the port as 5000
4. the 'db_service' should be built from image 'redis:alpine'
5. Add a volume to the 'python_web_app' service that mounts the current directory to the working directory inside the container and set the flask envireonment in debug mode.
6. Visit 'http:localhost:5000" to check weather the Flask application is running fine.

The container name of 'python_web_app' service should be 'web_container' and container name of 'db_service' should be 'db_container'




Here are the instructions for each of the tasks mentioned:


Create a file named Dockerfile and add the following content:

# Base image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Set Flask environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_RUN_PORT=5000
ENV FLASK_ENV=development
# Expose port 5000
EXPOSE 5000
# Start Flask application
CMD ["flask", "run"]




Create a file named docker-compose.json and add the following content:

{

  "version": "3",

  "services": {

    "python_web_app": {

      "build": {

        "context": ".",

        "dockerfile": "Dockerfile"

      },

      "ports": [

        "5000:5000"

      ],

      "volumes": [

        ".:/app"

      ],

      "environment": {

        "FLASK_ENV": "development"

      },

      "container_name": "web_container"

    },

    "db_service": {

      "image": "redis:alpine",

      "container_name": "db_container"

      "volumes": [

        "redis-data:/data" //added to retain the data after docker-compose down

      ],

      "ports": [

        "6379:6379" //this port declaration is optional, not defined, not used locally

      ]

    }

  },

  "volumes": {

    "redis-data": {}

  }

}


Build and run the Docker containers:
Assuming you have Docker installed and the terminal is in the same directory as the Dockerfile and docker-compose.json files, run the following commands:

# Build and run the containers in detached mode
docker-compose -f docker-compose.json up -d

Verify Flask application:
Open your web browser and visit http://localhost:5000 to check if the Flask application is running fine.


---THANK YOU---

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