Introduction
Docker Compose permits customers to run and outline multi-container functions utilizing a single configuration file. It simplifies the method of organising and managing a number of containers, making it simpler to develop, take a look at, and deploy functions.
On this article, you might be to create a Flask software with two containers, use Vultr Container Registry (VCR) to handle your functionsβ Docker picture, and make the most of Docker Composeβs multi-container performance to handle a number of containers.
Creating an Instance Utility
To get began with creating an instance software, observe these steps:
- Deploy a Vultr Compute occasion utilizing the Vultr Customer Portal with Docker market software.
- Securely access the server using SSH as a non-root sudo user.
- Update the server.
- Create a brand new mission listing and navigate into it.
- Create a brand new file named
app.py
. - Add the next code.
Save and exit the file.
The above Flask code connects to a Redis Database and increments the counter each time the foundation URL is visited.
- Enable incoming connections to port
5000
and reload the firewall. - Create a brand new file named
necessities.txt
. - Add the next packages.
Save and shut the file.
- Create one other listing contained in the
flask-redis-example
listing and navigate into it. - Create a brand new file named
types.css
. - Add the next code.
Save and exit the file.
- Create one other listing contained in the
flask-redis-example
listing and navigate into it. - Create a brand new file named
index.html
. - Add the next code.
Save and exit the file.
Utilizing the Vultr Container Registry
On this part, you might be to create a Vultr Container Registry, add your Docker picture to the registry, and arrange a Docker Compose file organising companies for the Flask and the Redis database.
- Deploy a Vultr Container Registry
- Create a Docker manifest within the
flask-redis-example
listing. - Add the next configuration.
Save and exit the file.
- Construct the Docker picture.
- Log in to your Vultr Container Registry.
Be sure to exchange
<url>
,<consumer>
, and<password>
, these particulars are supplied within the overview part of your Vultr Container Registry. - Tag the Docker picture.
- Push the picture to the Vultr Container Registry.
As soon as the Docker picture has been pushed, confirm the picture presence within the Repositories part in your Vultr Container Registry on the Vultr Dashboard.
- Create a brand new file named
docker-compose.yaml
.Save and exit the file.
The above YAML configuration defines two companies
net
andredis
. Thenet
service builds the Flask app from the present listing (.
) and maps port5000
of the container to port5000
of the host. It additionally specifies that the net service depends upon the Redis service. The Redis service makes use of the official Redis Docker picture from the Docker Hub. - Construct the Docker compose file.
After the construct course of is accomplished entry the Flask app on
http://<server-ip>:5000
. Attempt refreshing the web site a number of instances and observe that the depend of the variety of instances the web page visited is growing.
Do extra with the Vultr Container Registry
Greatest Practices
- Preserving the
docker-compose.yaml
file organized and effectively documented. - Utilizing named volumes for persisting knowledge as a substitute of binding host directories.
- Utilizing atmosphere variables for delicate knowledge like passwords and API keys.
- Utilizing Docker Composeβs built-in instructions like
docker-compose up
,docker-compose down
, anddocker-compose
ps to handle containers.
Conclusion
On this article, you created a Flask software with two containers, used Vultr Container Registry to handle your functionsβs Docker picture, and utilized Docker Composeβs multi-container performance to handle a number of containers.
This can be a sponsored article by Vultr. Vultr is the worldβs largest privately-held cloud computing platform. A favourite with builders, Vultr has served over 1.5 million prospects throughout 185 international locations with versatile, scalable, international Cloud Compute, Cloud GPU, Naked Metallic, and Cloud Storage options. Be taught extra about Vultr