Thursday, 30 January 2025

3 - Website Maintenance - Use case

 Let's walk through a simplified demo of using Docker for website maintenance. This example focuses on updating a website without downtime.

Scenario: You have a simple website running in a Docker container. You need to update the website content. We'll use a static website (HTML, CSS, JS) for simplicity, but the principles apply to dynamic websites as well.

Prerequisites:

  • Docker installed.
  • Basic understanding of Docker commands.

Steps:

  1. Initial Setup (Creating the Website and Image):

    • Create Website Files: Create a directory (e.g., my-website). Inside, create an index.html file with some basic HTML content:

      HTML
      <!DOCTYPE html>
      <html>
      <head>
          <title>My Website</title>
      </head>
      <body>
          <h1>Welcome to My Website (Version 1)</h1>
      </body>
      </html>
      
    • Create a Dockerfile: In the my-website directory, create a Dockerfile:

      Dockerfile
      FROM nginx:latest  # Use Nginx as a web server
      COPY . /usr/share/nginx/html  # Copy website files into the container
      
    • Build the Image: Open your terminal in the my-website directory and build the Docker image:

      Bash
      docker build -t my-website-image:v1 .
      
    • Run the Container:

      Bash
      docker run -p 8080:80 my-website-image:v1
      

      Now, if you visit http://localhost:8080 in your browser, you should see your website.

  2. Website Update:

    • Modify Website Files: Update the index.html file in your my-website directory. For example, change the heading:

      HTML
      <h1>Welcome to My Website (Version 2)</h1>
      
    • Build a New Image: Build a new Docker image with a different tag:

      Bash
      docker build -t my-website-image:v2 .
      
  3. Zero-Downtime Deployment (using a second container):

    • Run the New Container on a Different Port: Run the new container on a different port:

      Bash
      docker run -p 8081:80 my-website-image:v2
      

      Now, if you visit http://localhost:8081, you'll see the updated website. Your original website is still running on http://localhost:8080.

    • Switch Traffic (using a reverse proxy - Nginx example): This is the key to zero-downtime. You'll use a reverse proxy (like Nginx) to direct traffic to either the old or the new container.

      • Run a Nginx Proxy Container: You'll need a separate Nginx configuration to act as a proxy. Here's a simplified example (nginx.conf):

        Nginx
        server {
            listen 80; # The port your users access
        
            location / {
                proxy_pass http://localhost:8080; # Initially point to the old container
            }
        }
        
      • Create a Dockerfile for the proxy:

        Dockerfile
        FROM nginx:latest
        COPY nginx.conf /etc/nginx/conf.d/default.conf
        

        Build and run the proxy container, mapping port 80 (the standard HTTP port):

        Bash
        docker build -t my-website-proxy .
        docker run -p 80:80 my-website-proxy
        
      • Update Nginx Configuration: This is the crucial step. You need to change the proxy_pass directive in your Nginx configuration to point to the new container (port 8081). You can do this by either:

        • Rebuilding the Proxy Container: Modify the nginx.conf file and rebuild the my-website-proxy image.
        • Using Volumes (more advanced): Mount the nginx.conf file as a volume in the proxy container, so you can update it without rebuilding the image.

      Once the proxy points to the new container, your users will seamlessly see the updated website.

    • Stop the Old Container: After you've verified the new website is working, you can stop and remove the old container:

      Bash
      docker stop <old_container_id>
      docker rm <old_container_id>
      

Key Improvements and Considerations:

  • Docker Compose: For more complex applications, use Docker Compose to manage multiple containers (website, database, proxy) together.
  • Automated Updates: Tools like Docker Swarm or Kubernetes can automate the process of updating containers and switching traffic.
  • Health Checks: Implement health checks in your Docker containers so that the orchestration tools know when a new container is ready to receive traffic.
  • Rolling Updates: Instead of switching traffic all at once, you can perform rolling updates, gradually updating containers to minimize disruption.
  • CI/CD Integration: Integrate Docker into your continuous integration and continuous deployment (CI/CD) pipeline to automate the entire process of building, testing, and deploying updates.

This demo provides a basic example. Real-world website maintenance with Docker involves more sophisticated techniques and tools, but the core principles of containerization and orchestration remain the same. Using Docker for website maintenance offers advantages like easy rollbacks, consistent environments, and reduced downtime.

No comments:

Post a Comment

Dockerize Agent -Document Processing

 Since you are using Flask in Agent.py , I will modify the Dockerfile and steps accordingly. [WIP] 1. Update the Dockerfile Create a Do...