search
HomeTechnology peripheralsIt IndustryThe Functional Depth of Docker and Docker Compose

The Functional Depth of Docker and Docker Compose

Build multi-container Flask application using Docker Compose and Vultr container registry

Docker Compose allows users to run and define multi-container applications using a single configuration file. It simplifies the process of setting up and managing multiple containers, making it easier to develop, test, and deploy applications. This article will guide you to create a Flask application with two containers, manage Docker images of your application using the Vultr Container Registry (VCR), and manage multiple containers with the multi-container functionality of Docker Compose.

Creation of sample application

The following steps will guide you to create a sample application:

  1. Deploy a Vultr compute instance using the Docker market application through the Vultr customer portal.

  2. Use SSH to securely access the server with non-root sudo users.

  3. Update the server.

  4. Create a new project directory and enter it:

    mkdir flask-redis-example
    cd flask-redis-example
  5. Create a new file named app.py:

    nano app.py
  6. Add the following code:

    from flask import Flask, render_template
    import redis
    
    app = Flask(__name__)
    redis_client = redis.Redis(host='redis', port=6379)
    
    @app.route('/')
    def hello():
        count = redis_client.incr('hits')
        return render_template('index.html', count=count)
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)

    Save and exit the file. This Flask code connects to the Redis database and increments the counter every time the root URL is accessed.

  7. Allow incoming connections to port 5000 and reloading the firewall:

    sudo ufw allow 5000
    sudo ufw reload
  8. Create a new file named requirements.txt:

    nano requirements.txt
  9. Add the following package:

    <code>flask
    redis</code>

    Save and close the file.

  10. Create another directory in the flask-redis-example directory and enter it:

     mkdir static
     cd static
  11. Create a new file named styles.css:

     nano styles.css
  12. Add the following code:

     body {
         font-family: Arial, sans-serif;
         text-align: center;
         background-color: #f4f4f4;
         margin: 0;
         padding: 0;
     }
    
     h1 {
         color: #333;
         margin-top: 50px;
     }
    
     p {
         font-size: 18px;
         color: #666;
     }

    Save and exit the file.

  13. Create another directory in the flask-redis-example directory and enter it:

     mkdir templates
     cd templates
  14. Create a new file named index.html:

     nano index.html
  15. Add the following code:

     <!DOCTYPE html>
     <html>
     <head>
         <title>Flask App</title>
         <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
     </head>
     <body>
         <h1 id="Hello-World">Hello, World!</h1>
         <p>I have been seen {{ count }} times.</p>
     </body>
     </html>

    Save and exit the file.

Using Vultr Container Registry

In this section, you will create a Vultr container registry, upload your Docker image to the registry, and set up a Docker Compose file to set up the services of the Flask and Redis databases.

  1. Deploy a Vultr container registry.

  2. Create a Docker manifest in the flask-redis-example directory:

    nano Dockerfile.flask
  3. Add the following configuration:

    FROM python:3.9-slim
    
    WORKDIR /app
    
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY app.py .
    COPY static/ ./static/
    COPY templates/ ./templates/
    
    EXPOSE 5000
    
    CMD ["python", "app.py"]

    Save and exit the file.

  4. Build Docker image:

    docker build -t flask-app .
  5. Login to your Vultr container registry:

    docker login <url> -u <user> -p <password>

    Be sure to replace <url></url>, <user></user> and <password></password>, which are provided in the Overview section of your Vultr Container Registry.

  6. Tag Docker image:

    mkdir flask-redis-example
    cd flask-redis-example
  7. Pusing the mirror to the Vultr container registry:

    nano app.py

    After pushing the Docker image, verify that the image exists in the "Repository" section of the Vultr container registry on the Vultr dashboard.

  8. Create a new file named docker-compose.yaml:

    from flask import Flask, render_template
    import redis
    
    app = Flask(__name__)
    redis_client = redis.Redis(host='redis', port=6379)
    
    @app.route('/')
    def hello():
        count = redis_client.incr('hits')
        return render_template('index.html', count=count)
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)

    Save and exit the file. The above YAML configuration defines two services web and redis. web Service builds the Flask application from the current directory (.) and maps the container's port 5000 to the host's port 5000. It also specifies that the web service depends on the redis service. redisThe service uses the official Redis Docker image from Docker Hub.

  9. Build Docker Compose file:

    sudo ufw allow 5000
    sudo ufw reload

    After the build process is completed, visit the Flask application on http://:5000. Try refreshing the website multiple times and observe whether the count of page visits increases.

More use of Vultr container registry

  • Vultr Container Registry with Docker
  • Vultr Container Registry with Kubernetes
  • Build vLLM container image
  • Build Llama.cpp container image
  • Build PyTorch container image

Best Practice

  • Keep docker-compose.yamlThe files are well organized and well documented.
  • Use named volumes to persist data instead of binding to host directory.
  • Use environment variables to store sensitive data such as passwords and API keys.
  • Use the built-in commands of Docker Compose, such as docker-compose up, docker-compose down, and docker-compose ps to manage containers.

Conclusion

In this article, you created a Flask application with two containers, used the Vultr container registry to manage the Docker image of the application, and took advantage of the multi-container functionality of Docker Compose to manage multiple containers.

This article is sponsored by Vultr. Vultr is the world's largest private cloud computing platform. Favorite among developers, Vultr has provided flexible and scalable global cloud computing, cloud GPU, bare metal and cloud storage solutions to more than 1.5 million customers in 185 countries. Learn more about Vultr

The above is the detailed content of The Functional Depth of Docker and Docker Compose. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Top 21 Developer Newsletters to Subscribe To in 2025Top 21 Developer Newsletters to Subscribe To in 2025Apr 24, 2025 am 08:28 AM

Stay informed about the latest tech trends with these top developer newsletters! This curated list offers something for everyone, from AI enthusiasts to seasoned backend and frontend developers. Choose your favorites and save time searching for rel

Serverless Image Processing Pipeline with AWS ECS and LambdaServerless Image Processing Pipeline with AWS ECS and LambdaApr 18, 2025 am 08:28 AM

This tutorial guides you through building a serverless image processing pipeline using AWS services. We'll create a Next.js frontend deployed on an ECS Fargate cluster, interacting with an API Gateway, Lambda functions, S3 buckets, and DynamoDB. Th

CNCF Arm64 Pilot: Impact and InsightsCNCF Arm64 Pilot: Impact and InsightsApr 15, 2025 am 08:27 AM

This pilot program, a collaboration between the CNCF (Cloud Native Computing Foundation), Ampere Computing, Equinix Metal, and Actuated, streamlines arm64 CI/CD for CNCF GitHub projects. The initiative addresses security concerns and performance lim

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment