Home >Technology peripherals >It Industry >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:
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:
<code class="language-bash">mkdir flask-redis-example cd flask-redis-example</code>
Create a new file named app.py
:
<code class="language-bash">nano app.py</code>
Add the following code:
<code class="language-python">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)</code>
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:
<code class="language-bash">sudo ufw allow 5000 sudo ufw reload</code>
Create a new file named requirements.txt
:
<code class="language-bash">nano requirements.txt</code>
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:
<code class="language-bash"> mkdir static cd static</code>
Create a new file named styles.css
:
<code class="language-bash"> nano styles.css</code>
Add the following code:
<code class="language-css"> 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; }</code>
Save and exit the file.
Create another directory in the flask-redis-example
directory and enter it:
<code class="language-bash"> mkdir templates cd templates</code>
Create a new file named index.html
:
<code class="language-bash"> nano index.html</code>
Add the following code:
<code class="language-html"> <!DOCTYPE html> <title>Flask App</title> <link rel="stylesheet" href="%7B%7B%20url_for('static',%20filename='styles.css')%20%7D%7D"> <h1>Hello, World!</h1> <p>I have been seen {{ count }} times.</p> </code>
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:
<code class="language-bash">nano Dockerfile.flask</code>
Add the following configuration:
<code class="language-dockerfile">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"]</code>
Save and exit the file.
Build Docker image:
<code class="language-bash">docker build -t flask-app .</code>
Login to your Vultr container registry:
<code class="language-bash">docker login <url> -u <user> -p <password></password></user></url></code>
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:
<code class="language-bash">mkdir flask-redis-example cd flask-redis-example</code>
Pusing the mirror to the Vultr container registry:
<code class="language-bash">nano app.py</code>
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
:
<code class="language-python">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)</code>
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:
<code class="language-bash">sudo ufw allow 5000 sudo ufw reload</code>
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!