26 C
New York
Thursday, June 27, 2024

The Useful Depth of Docker and Docker Compose — SitePoint


Introduction

Docker Compose permits customers to run and outline multi-container purposes utilizing a single configuration file. It simplifies the method of establishing and managing a number of containers, making it simpler to develop, check, and deploy purposes.

On this article, you might be to create a Flask utility with two containers, use Vultr Container Registry (VCR) to handle your purposes’ 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 utility, observe these steps:

  1. Deploy a Vultr Compute occasion utilizing the Vultr Buyer Portal with Docker market utility.
  2. Securely entry the server utilizing SSH as a non-root sudo person.
  3. Replace the server.
  4. Create a brand new undertaking listing and navigate into it.
  5. Create a brand new file named app.py.
  6. 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 basis URL is visited.

  7. Permit incoming connections to port 5000 and reload the firewall.
  8. Create a brand new file named necessities.txt.
  9. Add the next packages.

    Save and shut the file.

  10. Create one other listing contained in the flask-redis-example listing and navigate into it.
  11. Create a brand new file named kinds.css.
  12. Add the next code.

    Save and exit the file.

  13. Create one other listing contained in the flask-redis-example listing and navigate into it.
  14. Create a brand new file named index.html.
  15. 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 establishing companies for the Flask and the Redis database.

  1. Deploy a Vultr Container Registry
  2. Create a Docker manifest within the flask-redis-example listing.
  3. Add the next configuration.

    Save and exit the file.

  4. Construct the Docker picture.
  5. Log in to your Vultr Container Registry.

    Ensure that to exchange <url>, <person>, and <password>, these particulars are supplied within the overview part of your Vultr Container Registry.

  6. Tag the Docker picture.
  7. 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.

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

    Save and exit the file.

    The above YAML configuration defines two companies net and redis. The net service builds the Flask app from the present listing (.) and maps port 5000 of the container to port 5000 of the host. It additionally specifies that the net service is dependent upon the Redis service. The Redis service makes use of the official Redis Docker picture from the Docker Hub.

  9. 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 occasions and observe that the rely of the variety of occasions the web page visited is rising.

Do extra with the Vultr Container Registry

Greatest Practices

  • Retaining the docker-compose.yaml file organized and nicely documented.
  • Utilizing named volumes for persisting knowledge as a substitute of binding host directories.
  • Utilizing surroundings variables for delicate knowledge like passwords and API keys.
  • Utilizing Docker Compose’s built-in instructions like docker-compose up, docker-compose down, and docker-compose ps to handle containers.

Conclusion

On this article, you created a Flask utility with two containers, used Vultr Container Registry to handle your purposes’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 clients throughout 185 international locations with versatile, scalable, international Cloud Compute, Cloud GPU, Naked Metallic, and Cloud Storage options. Study extra about Vultr



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles