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:
-
Deploy a Vultr compute instance using the Docker market application through the Vultr customer portal.
-
Use SSH to securely access the server with non-root sudo users.
-
Update the server.
-
Create a new project directory and enter it:
mkdir flask-redis-example cd flask-redis-example
-
Create a new file named
app.py
:nano app.py
-
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.
-
Allow incoming connections to port 5000 and reloading the firewall:
sudo ufw allow 5000 sudo ufw reload
-
Create a new file named
requirements.txt
:nano requirements.txt
-
Add the following package:
<code>flask redis</code>
Save and close the file.
-
Create another directory in the
flask-redis-example
directory and enter it:mkdir static cd static
-
Create a new file named
styles.css
:nano styles.css
-
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.
-
Create another directory in the
flask-redis-example
directory and enter it:mkdir templates cd templates
-
Create a new file named
index.html
:nano index.html
-
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.
-
Deploy a Vultr container registry.
-
Create a Docker manifest in the
flask-redis-example
directory:nano Dockerfile.flask
-
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.
-
Build Docker image:
docker build -t flask-app .
-
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. -
Tag Docker image:
mkdir flask-redis-example cd flask-redis-example
-
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.
-
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
andredis
.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 theweb
service depends on theredis
service.redis
The service uses the official Redis Docker image from Docker Hub. -
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.yaml
The 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
, anddocker-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!

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
