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>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>
, <user>
and <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
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. 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://
More use of Vultr container registry
Best Practice
docker-compose.yaml
The files are well organized and well documented. 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!