Adventures in Machine Learning

Unleashing the Power of Docker: Deploying a Flask App to the Cloud

Dockerizing a Flask App with Docker Compose

In the world of software development and deployment, Docker has emerged as a revolutionary technology. Using Docker containers, developers can build, ship, and run applications on any platform, regardless of dependencies, without the worry of compatibility issues.

However, diving into Docker and containerization can be daunting, especially for those new to the technology. In this article, we will discuss how to set up Docker on your local machine, how to use Docker Compose to containerize a Flask app, and how to create and access a database within the container.

Installing Docker Compose and Machine

Before diving into creating Docker containers, we must first set up the necessary tools, including Docker Compose and Machine. Docker Compose is a tool for defining and running multi-container Docker applications, while Docker Machine is a tool that automatically sets up a Docker environment on a local or remote host computer.

To install both tools, you can use the following commands in your terminal:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo install /tmp/docker-machine /usr/local/bin/docker-machine

Project Structure and Cloning Repository

Now that we have the necessary tools installed, let’s move on to creating a Flask app for containerization. You can create your Flask app in any manner you prefer, but for this article, we will use the default Flask app structure.

To create the structure, create a new directory and run the following commands:

$ mkdir my_flask_app
$ cd my_flask_app
$ touch app.py

After creating the my_flask_app directory, clone the Flask-MySQL-User-Management app repository from GitHub:

$ git clone https://github.com/deepansha03/Flask-MySQL-User-Management.git

Setting up Docker Machine

Now that we have the project directory and the Flask app repository cloned, let’s set up a Docker Machine instance. We will create a virtual machine called dev using the following command:

$ docker-machine create --driver virtualbox dev

After executing this command, you should see output detailing the progress and completion of the machine creation.

Verifying that the machine is active can be done by running:

$ docker-machine ls

Using Docker Compose to Containerize a Flask App

The next step is to use Docker Compose to define a multi-container Docker application and containerize the Flask app. Before we do that, we need to create a Dockerfile and a docker-compose.yml file.

For the Dockerfile, create a new file within the Flask app directory my_flask_app:

$ cd Flask-MySQL-User-Management
$ touch Dockerfile

In the Dockerfile, add the following code:

FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

EXPOSE 5000
CMD ["python", "app.py"]

This code specifies that we will be using the Python version 3 image as our base image and installs dependencies from the requirements.txt file. This file should exist as it is part of the cloned Flask app repository.

It also exposes the port 5000 that the Flask app will run on and defines the command to start the app.

Next, create the docker-compose.yml file in the same directory:

$ cd ..

$ touch docker-compose.yml

Inside the docker-compose.yml file, add the following configuration:

version: '3'
services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: user_db
    ports:
      - "3307:3306"
  web:
    build: .
    command: python app.py
    volumes:
      - .:/code
    ports:
      - "5000:5000"
    depends_on:
      - db

Understanding the docker-compose.yml file

In the docker-compose.yml file, we define two services: db and web.

The db service is a MySQL database that we will use to store user information. Docker will pull the latest MySQL Docker image and create a database instance.

We expose the database port 3306 as port 3307 on the Docker Machine host. The web service corresponds to our Flask app.

We build the image from the current directory using the . notation, run the command to start our Flask app, and expose the Flask app port, 5000. The volumes parameter is used to map the current directory to the code directory of the Docker container, so changes are reflected immediately.

The depends_on parameter specifies that this service depends on the db service, so the database is started before the Flask app.

Building and Running Containers with Docker Compose

Now we’re ready to build and run our Docker containers. Run the following command:

$ docker-compose up --build

This command creates and starts both containers for the db and web services.

You should see output in the terminal showing the progress of the Docker container creation process. Once the terminal shows that the Flask app has started on port 5000, you can access the app by visiting http://(DOCKER MACHINE IP):5000 in your browser.

Creating and Accessing a Database

To create and access a database within the container, we can use the mysql command-line interface. First, connect to the db container:

$ docker-compose exec db mysql -u root -p

You will be prompted to enter a password.

The default password is “`password`”. Now that we are connected, let’s create a database and a user:

mysql> CREATE DATABASE user_db;
mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%';

This code creates a database called user_db and a user named user with the password “`password`”.

The user is also granted all privileges on all databases.

Conclusion

Docker is a powerful tool that enables developers to containerize their applications and deploy them seamlessly on any platform. By following the steps provided in this article, you can set up a Flask app using Docker Compose and ensure that your app runs in a container without compatibility issues.

Containerization also ensures that deployed apps are isolated from dependencies and external factors, ensuring fewer deployment errors and faster deployment times. With Docker, deploying your applications has never been simpler.

Deployment using Docker Machine

We have seen how to containerize a Flask app using Docker Compose in the previous section. In this section, we will explore how we can deploy our application to the cloud using Docker Machine.

Docker Machine allows you to create Docker hosts on your local machine or in a cloud host of your choice, like AWS, Microsoft Azure, or Digital Ocean.

Creating a Digital Ocean Droplet with Docker Machine

For this section, we will focus on Digital Ocean as our cloud hosting provider. First, we need to create an account on Digital Ocean and create a personal access token.

Once we have our access token, we can use it to create a Docker Machine with Digital Ocean using the following command:

docker-machine create --driver digitalocean --digitalocean-access-token ACCESS_TOKEN droplet-name

This command creates a new Droplet on Digital Ocean and sets up a Docker host using Docker Machine. Digital Ocean provides a clean and easy-to-use interface for creating, managing, and monitoring Droplets.

Building and Running Containers on Cloud Hosting

After setting up the Docker Machine instance, we need to build and run our containers on the cloud. To do that, we must connect our Docker client to the Docker Machine instance running on Digital Ocean.

To connect to the instance, use the following command:

eval $(docker-machine env droplet-name)

Once connected, we need to build and run our containers using the same docker-compose up --build command as before, but with an additional parameter to specify the docker-compose.yml file location:

docker-compose -f /path/to/docker-compose.yml up --build

This command tells Docker Compose to use the specified docker-compose.yml file and build and run the containers. The app should now be running on the cloud!

Viewing the App on Browser

To view the app on the browser, we need to obtain the IP address of the Droplet running the containers. We can do this using the following command:

docker-machine ip droplet-name

This command displays the IP address of the Droplet. Simply enter the IP address into a web browser advanced to the defined app port, and the Flask app should be active and ready to use.

Using multiple Docker containers and Load Balancing

Although we have successfully deployed our Flask app to Digital Ocean using Docker Machine, it is a best practice to use multiple Docker containers and load balancing. Multiple containers distribute the load among themselves, making it possible to handle high traffic without any downtime.

To do this, we need to make some changes to our docker-compose.yml file. Instead of using a single instance of the Flask app container, we can define multiple instances as follows:

version: '3'
services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: user_db
    ports:
      - "3307:3306"
  web:
    build: .

    command: gunicorn --bind 0.0.0.0:8000 wsgi:app
    volumes:
      - .:/code
    scale: 3
    environment:
      - PORT=8000
    depends_on:
      - db
  lb:
    image: dockersamples/nginx-loadbalancer
    ports:
      - "80:80"
    links:
      - web

In the web service, we add the scale parameter to indicate the number of instances we want to run. In this case, we set it to three.

We also update the command parameter to start the Gunicorn server, which can handle multiple requests in parallel. We also need to define a unique PORT environment variable.

In addition to the web service, we include a new lb service that is responsible for load balancing the incoming requests among the web instances using the Nginx-loadbalancer image. We expose port 80 of the load balancer service to the Docker Machine host.

Github Repository for the Code

Finally, we can update the code on a GitHub repository to make it easier to share and collaborate with other developers. We can use GitHub as a code repository for our Flask app, as well as its Docker scripts.

For example, suppose we store the docker-compose.yml, Dockerfile, and Flask code in a repository called my-flask-app. In that case, other developers can quickly clone the repository and use the same setup to build and deploy the Flask app.

Conclusion and Future Extensions

In this article, we’ve covered how to use Docker Machine to set up a Docker host on Digital Ocean and deploy our Flask app using Docker Compose. Furthermore, we’ve explored how to use multiple containers and load balancing to handle high traffic loads.

We also touched on the importance of creating a Github repository for our code, which makes sharing and collaborating much easier. Our future extensions could include adding more Docker containers to handle other functionality, such as mail servers and databases.

Docker provides a clean and efficient way to deploy applications, and Docker Machine provides rapid deployment capabilities for cloud hosting. By using these tools together, developers can deploy their applications within seconds and be sure that they are stable and scalable.

In this article, we have discussed how developers can use Docker to containerize their applications and deploy them seamlessly on any platform. We started by introducing Docker and setting it up on our local machines using Docker Compose and Docker Machine.

We then explored how we can deploy our Flask application to the cloud using Docker Machine, specifically Digital Ocean. We learned how using multiple containers and load balancing can help handle high traffic and distribute the load, thereby increasing scalability.

Finally, we touched on the importance of creating a GitHub repository for our code to enable easy sharing and collaboration with other developers. Docker is a powerful tool that simplifies the deployment of applications and makes them easily scalable, and we should all embrace it as a vital tool in application development.

Popular Posts