Home > Technology peripherals > It Industry > The Functional Depth of Docker and Docker Compose

The Functional Depth of Docker and Docker Compose

Lisa Kudrow
Release: 2025-02-08 09:06:11
Original
680 people have browsed it

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
    Copy after login
    Copy after login
  5. Create a new file named app.py:

    nano app.py
    Copy after login
    Copy after login
  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)
    Copy after login
    Copy after login

    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
    Copy after login
    Copy after login
  8. Create a new file named requirements.txt:

    nano requirements.txt
    Copy after login
  9. Add the following package:

    <code>flask
    redis</code>
    Copy after login

    Save and close the file.

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

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

     nano styles.css
    Copy after login
  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;
     }
    Copy after login

    Save and exit the file.

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

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

     nano index.html
    Copy after login
  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>Hello, World!</h1>
         <p>I have been seen {{ count }} times.</p>
     </body>
     </html>
    Copy after login

    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
    Copy after login
  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"]
    Copy after login

    Save and exit the file.

  4. Build Docker image:

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

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

    Be sure to replace <url>, <user> and <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
    Copy after login
    Copy after login
  7. Pusing the mirror to the Vultr container registry:

    nano app.py
    Copy after login
    Copy after login

    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)
    Copy after login
    Copy after login

    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
    Copy after login
    Copy after login

    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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template