Introduction
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.
In this article, you are to create a Flask application with two containers, use Vultr Container Registry (VCR) to manage your applications’ Docker image, and utilize Docker Compose’s multi-container functionality to manage multiple containers.
Creating an Example Application
To get started with creating an example application, follow these steps:
- Deploy a Vultr Compute instance using the Vultr Customer Portal with Docker marketplace application.
- Securely access the server using SSH as a non-root sudo user.
- Update the server.
- Create a new project directory and navigate into 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.
The above Flask code connects to a Redis Database and increments the counter every time the root URL is visited.
- Allow incoming connections to port
5000
and reload the firewall.$ sudo ufw allow 5000 $ sudo ufw reload
- Create a new file named
requirements.txt
.$ nano requirements.txt
- Add the following packages.
flask redis
Save and close the file.
- Create another directory inside the
flask-redis-example
directory and navigate into 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 inside the
flask-redis-example
directory and navigate into 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 the Vultr Container Registry
In this section, you are to create a Vultr Container Registry, upload your Docker image to the registry, and set up a Docker Compose file setting up services for the Flask and the Redis database.
- 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 the Docker image.
$ docker build -t flask-app .
- Log in to your Vultr Container Registry.
$ docker login <url> -u <user> -p <password>
Make sure to replace
<url>
,<user>
, and<password>
, these details are provided in the overview section of your Vultr Container Registry. - Tag the Docker image.
$ docker tag flask-app:latest <url>flask-app:latest
- Push the image to the Vultr Container Registry.
$ docker push <url>flask-app:latest
Once the Docker image has been pushed, verify the image presence in the Repositories section in your Vultr Container Registry on the Vultr Dashboard.
- Create a new file named
docker-compose.yaml
.version: '3' services: web: image: <url>/flask-app:latest ports: - "5000:5000" depends_on: - redis redis: image: "redis:alpine"
Save and exit the file.
The above YAML configuration defines two services
web
andredis
. Theweb
service builds the Flask app from the current directory (.
) and maps port5000
of the container to port5000
of the host. It also specifies that the web service depends on the Redis service. The Redis service uses the official Redis Docker image from the Docker Hub. - Build the Docker compose file.
$ docker-compose up --build
After the build process is completed access the Flask app on
http://<server-ip>:5000
. Try refreshing the website multiple times and observe that the count of the number of times the page visited is increasing.
Do more with the Vultr Container Registry
- Vultr Container Registry with Docker
- Vultr Container Registry with Kubernetes
- Build a vLLM Container Image
- Build a Llama.cpp Container Image
- Build a PyTorch Container Image
Best Practices
- Keeping the
docker-compose.yaml
file organized and well documented. - Using named volumes for persisting data instead of binding host directories.
- Using environment variables for sensitive data like passwords and API keys.
- Using Docker Compose’s built-in commands like
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 Vultr Container Registry to manage your applications’s Docker image, and utilized Docker Compose’s multi-container functionality to manage multiple containers.
This is a sponsored article by Vultr. Vultr is the world’s largest privately-held cloud computing platform. A favorite with developers, Vultr has served over 1.5 million customers across 185 countries with flexible, scalable, global Cloud Compute, Cloud GPU, Bare Metal, and Cloud Storage solutions. Learn more about Vultr
Vultr is the world’s largest privately-held cloud computing platform. A favorite with developers, Vultr has served over 1.5 million customers across 185 countries with flexible, scalable, global Cloud Compute, Cloud GPU, Bare Metal, and Cloud Storage solutions.