reCAPTCHA WAF Session Token
Programming Languages

The Functional Depth of Docker and Docker Compose — SitePoint


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:

  1. Deploy a Vultr Compute instance using the Vultr Customer Portal with Docker marketplace application.
  2. Securely access the server using SSH as a non-root sudo user.
  3. Update the server.
  4. Create a new project directory and navigate into it.
  5. Create a new file named app.py.
  6. Add the following code.

    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.

  7. Allow incoming connections to port 5000 and reload the firewall.
  8. Create a new file named requirements.txt.
  9. Add the following packages.

    Save and close the file.

  10. Create another directory inside the flask-redis-example directory and navigate into it.
  11. Create a new file named styles.css.
  12. Add the following code.

    Save and exit the file.

  13. Create another directory inside the flask-redis-example directory and navigate into it.
  14. Create a new file named index.html.
  15. Add the following code.

    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.

  1. Deploy a Vultr Container Registry
  2. Create a Docker manifest in the flask-redis-example directory.
  3. Add the following configuration.

    Save and exit the file.

  4. Build the Docker image.
  5. Log in to your Vultr Container Registry.

    Make sure to replace , , and , these details are provided in the overview section of your Vultr Container Registry.

  6. Tag the Docker image.
  7. Push the image to the Vultr Container Registry.

    Once the Docker image has been pushed, verify the image presence in the Repositories section in your Vultr Container Registry on the Vultr Dashboard.

  8. Create a new file named docker-compose.yaml.

    Save and exit the file.

    The above YAML configuration defines two services web and redis. The web service builds the Flask app from the current directory (.) and maps port 5000 of the container to port 5000 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.

  9. Build the Docker compose file.

    After the build process is completed access the Flask app on http://: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

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, and docker-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

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock