The Functional Depth of Docker and Docker Compose

Share this article

The Functional Depth of Docker and Docker Compose

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.
    $ mkdir flask-redis-example
    $ cd flask-redis-example
  5. Create a new file named app.py.
    $ nano app.py
  6. 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.

  7. Allow incoming connections to port 5000 and reload the firewall.
    $ sudo ufw allow 5000
    $ sudo ufw reload
  8. Create a new file named requirements.txt.
    $ nano requirements.txt
  9. Add the following packages.
    flask
    redis

    Save and close the file.

  10. Create another directory inside the flask-redis-example directory and navigate into it.
    $ mkdir static
    $ cd static
  11. Create a new file named styles.css.
    $ nano styles.css
  12. 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.

  13. Create another directory inside the flask-redis-example directory and navigate into it.
    $ mkdir templates
    $ cd templates
  14. Create a new file named index.html.
    $ nano index.html
  15. 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.

  1. Deploy a Vultr Container Registry
  2. Create a Docker manifest in the flask-redis-example directory.
    $ nano Dockerfile.flask
  3. 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 [&quot;python&quot;, &quot;app.py&quot;]

    Save and exit the file.

  4. Build the Docker image.
    $ docker build -t flask-app .
  5. Log in to your Vultr Container Registry.
    $ docker login &lt;url&gt; -u &lt;user&gt; -p &lt;password&gt;

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

  6. Tag the Docker image.
    $ docker tag flask-app:latest &lt;url&gt;flask-app:latest
  7. Push the image to the Vultr Container Registry.
    $ docker push &lt;url&gt;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.

  8. 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 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.
    $ 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

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

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.

flasksponsored
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week