Debug School

rakesh kumar
rakesh kumar

Posted on

Different way to deploy your web application in production using docker

Different approach to deploy your web application in production using docker

Deploy a Python CRUD web application in production using Docker containers

Here's a common approach to deploying web applications in production using Docker and Kubernetes:

Dockerizing the Application:

Create a Dockerfile for your web application, specifying the necessary dependencies and configurations.
Build a Docker image for your application using the Docker CLI or a build system like Docker Compose.
Example Dockerfile:

FROM node:14-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

Docker Registry:

Push your Docker image to a container registry such as Docker Hub, Google Container Registry, or an on-premises registry.

docker build -t your-image-name:tag .
docker push your-image-name:tag
Enter fullscreen mode Exit fullscreen mode

Kubernetes Deployment:

Create Kubernetes YAML files to define the deployment, services, and other resources.
Example Deployment YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
      - name: your-app
        image: your-image-name:tag
        ports:
        - containerPort: 3000
Enter fullscreen mode Exit fullscreen mode

Apply the YAML files to create and manage the application in the Kubernetes cluster.

kubectl apply -f your-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Load Balancing and Service Discovery:

Use Kubernetes Services to expose your application, providing load balancing and service discovery.
Example Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: your-app
spec:
  selector:
    app: your-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

Apply the YAML file to create the service.

kubectl apply -f your-service.yaml
Enter fullscreen mode Exit fullscreen mode

Scaling and Rolling Updates:

Utilize Kubernetes features for scaling your application horizontally (replica scaling) and deploying rolling updates without downtime.
Example Scaling:

kubectl scale deployment your-app --replicas=5
Enter fullscreen mode Exit fullscreen mode

Example Rolling Update:

kubectl set image deployment/your-app your-app=your-new-image:tag
Enter fullscreen mode Exit fullscreen mode

Monitoring and Logging:

Implement monitoring and logging solutions compatible with Kubernetes to gain insights into the performance and behavior of your application.
This approach provides a scalable, reliable, and maintainable way to deploy web applications in a production environment using Docker and Kubernetes. Keep in mind that the technology landscape evolves, and it's advisable to check for the latest best practices and tools as of your current date.

Deploy a Python CRUD web application in production using Docker containers

Step 1: Dockerfile
Create a Dockerfile in your project's root directory. This file defines the environment and dependencies needed for your Python application.

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN apt-get update && \
    apt-get install -y libpq-dev && \
    pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variables
ENV POSTGRES_DB your_database
ENV POSTGRES_USER your_user
ENV POSTGRES_PASSWORD your_password
ENV POSTGRES_HOST db
ENV POSTGRES_PORT 5432

# Start the application
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "your_app:app"]
Enter fullscreen mode Exit fullscreen mode

This example assumes you are using Gunicorn as the application server. Adjust the CMD line based on how your application is run.

Step 2: Docker Compose
Create a docker-compose.yml file to define the services, networks, and volumes for your application. Make sure to include a PostgreSQL service.

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:5000"
    depends_on:
      - db
  db:
    image: postgres:12
    environment:
      POSTGRES_DB: your_database
      POSTGRES_USER: your_user
      POSTGRES_PASSWORD: your_password
    ports:
      - "5432:5432"
Enter fullscreen mode Exit fullscreen mode

Step 3: Database Configuration
Update your Python application's database configuration to use the PostgreSQL service defined in the docker-compose.yml file.

Example configuration using SQLAlchemy

SQLALCHEMY_DATABASE_URI = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@db:5432/{POSTGRES_DB}"
Enter fullscreen mode Exit fullscreen mode

Step 4: Build and Run
Build and run your Docker containers using Docker Compose:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Step 5: Access the Application
Visit http://localhost:8080 in your browser to access your Python CRUD web application.

Step 6: Clean Up
When you're done, stop and remove the containers:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Adjust the configuration and structure according to your specific Python application. Consider using environment variables for sensitive information and adding security measures for production deployments, such as setting up SSL. Additionally, ensure that your Python application is configured for production, which might involve setting the FLASK_ENV environment variable to 'production' or adjusting other relevant settings.

Deploy a Php CRUD web application in production using Docker containers

Deploying a PHP CRUD web application with a PostgreSQL database using Docker involves several steps. Below is a step-by-step guide with examples to help you set up your PHP CRUD application with PostgreSQL using Docker containers.

Step 1: Dockerfile
Create a Dockerfile in your project's root directory. This file defines the environment and dependencies needed for your PHP application.

# Use an official PHP runtime as a parent image
FROM php:7.4-apache

# Set the working directory to /var/www/html
WORKDIR /var/www/html

# Copy the current directory contents into the container at /var/www/html
COPY . /var/www/html

# Install any needed packages specified in requirements.txt
RUN apt-get update && apt-get install -y \
    git \
    zip \
    unzip \
    libpq-dev

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install PHP extensions
RUN docker-php-ext-install pdo pdo_pgsql

# Install project dependencies
RUN composer install --no-dev --optimize-autoloader

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variables
ENV APACHE_DOCUMENT_ROOT /var/www/html/public

# Configure Apache
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Enable Apache rewrite module
RUN a2enmod rewrite

# Start Apache
CMD ["apache2-foreground"]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile is similar to the one in the previous example but includes the installation of the pdo_pgsql extension for PostgreSQL.

Step 2: Docker Compose
Create a docker-compose.yml file for defining the services, networks, and volumes for your application. Make sure to include a PostgreSQL service.

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db
  db:
    image: postgres:12
    environment:
      POSTGRES_DB: your_database
      POSTGRES_USER: your_user
      POSTGRES_PASSWORD: your_password
    ports:
      - "5432:5432"
Enter fullscreen mode Exit fullscreen mode

This docker-compose.yml file defines two services: web for your PHP application and db for PostgreSQL.

Step 3: Database Configuration
Make sure your PHP application's database configuration (config.php or similar) points to the PostgreSQL service defined in the docker-compose.yml file.

Step 4: Build and Run
Build and run your Docker containers using Docker Compose:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

This command will build the images and start the containers in detached mode.

Step 5: Access the Application
Visit http://localhost:8080 in your browser to access your PHP CRUD web application.

Step 6: Clean Up
When you're done, stop and remove the containers:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

This guide assumes a basic structure for a PHP CRUD application. Make sure to adjust it according to your specific project requirements. Additionally, consider adding security measures, such as using environment variables for sensitive information and configuring SSL for production deployments.

Top comments (0)