Home Software Development How To Deploy Flask Application With Docker On Ubuntu?

How To Deploy Flask Application With Docker On Ubuntu?

0
1215
deploy flask application, install Docker and create a Flask application on the Ubuntu server, deploy nodejs application, linux server

Flask is a lightweight Python web framework designed for easy to extend philosophy. It provides libraries and tools that allow you to build a web application. If you just want a static website for a restful web service that fits your mobile applications then Flask is the preferred choice for you. There are a lot of companies that use Flask in their tech stacks, including, Reddit, Netflix, and Lyft.

Docker is an open-source platform to deploy and test your application in a containerized environment. Docker allows you to deploy the application across multiple platforms and servers with minimal configuration. So It is a good idea to create a Flask application and deploy it with Docker.

Also Read: Cheat Sheet to Docker- Important Docker Commands for Software Developers

In this post, we will show you how to create & deploy Flask application with Docker on Ubuntu 20.04 server.

Requirements

  • A fresh system running Ubuntu 20.04 with a minimum of 4 GB RAM.
  • A root password is configured in your system.

Getting Started

Before starting, it is recommended to update your system package cache. You can update it with the following command:

apt-get update -y

Once all the system packages are updated, you can proceed to install the Docker.

Install Docker

By default, the latest version of Docker is not included in the Ubuntu standard repository. You will need to add the Docker official repository if you want to install the latest version of Docker.

First, install all the dependent packages using the following command:

apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg2 -y

Next, add the GPG key and repository with the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Once the repository is updated, install the latest version of Docker using the following command:

apt-get install docker-ce -y

After installing Docker, verify the installed version of Docker using the following command:

docker info

You should get brief information on Docker in the following output:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.2
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-29-generic
 Operating System: Ubuntu 20.04 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 3.844GiB
 Name: ubuntu2004
 ID: BPFH:SXB3:GRGI:NZF7:6M5X:TSPE:BUZY:XC74:MAIV:TMT7:2SO3:BXAT
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Create a Flask Application

In this section, we will create a directory structure for the Flask application then create a basic application.

First, create a directory named flaskapp to hold the Flask application.

mkdir ~/flaskapp

Next, change the directory to flaskapp and create a app.py file:

cd ~/flaskapp
nano app.py

Add the following content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Congratulations! you have successfully host Flask in a Docker container!'

if __name__ == "__main__":
    app.run(host ='0.0.0.0', debug = True)

Save and close the file when you are finished. Then, create a requirements.txt file to specify the Flask version that the pip package manager will install to your Docker environment:

nano requirements.txt

Add the latest version of Flask version as shown below:

Flask==1.1.2

Save and close the file when you are finished.

At the time of writing this tutorial, the latest version of Flask is 1.1.2. You can check the latest version at Flask

Create a Dockerfile

At this point, the Flask application has been set up successfully. You can now ready to setup Dockerfile to deploy it in the Docker environment.

Lets, create a Dockerfile in your project directory:

cd ~/flaskapp
nano Dockerfile

Add the following contents:

FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5001
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

Save and close the file when you are finished.

The above Dockerfile will download the python:alpine3.7 image from the Docker Hub repository, install the Flask application and copy the requirements.txt file into the container.

Build the Image and Start the Container

At this point, Dockerfile is ready to build the Docker image for the Flask application. 

Now, run the following command inside your project to build the Flask image using the Dockerfile:

cd ~/flaskapp
docker build -t flask-image:latest .

Once the Flask image has been built successfully, you should get the following output:

Removing intermediate container 5d6bfc3f375a
 ---> e66211619855
Step 5/7 : EXPOSE 5001
 ---> Running in bfb1393f692e
Removing intermediate container bfb1393f692e
 ---> 5687094c179f
Step 6/7 : ENTRYPOINT [ "python" ]
 ---> Running in 8327da8a8d6b
Removing intermediate container 8327da8a8d6b
 ---> a6d25ba20cd8
Step 7/7 : CMD [ "app.py" ]
 ---> Running in 0300c256f313
Removing intermediate container 0300c256f313
 ---> d9bd1b68d59c
Successfully built d9bd1b68d59c
Successfully tagged flask-image:latest

You can verify your image with the following command:

docker images

You should get the following output:

REPOSITORY    TAG         IMAGE ID       CREATED         SIZE
flask-image   latest      d9bd1b68d59c   4 minutes ago   91.6MB
python        alpine3.7   00be2573e9f7   24 months ago   81.3MB

You can now create and run the container from the Flask image using the following command:

docker container run -dit -p 5000:5000 flask-image

You can now verify your running container using the following command:

docker ps

You should get the following output:

CONTAINER ID   IMAGE         COMMAND           CREATED         STATUS         PORTS                              NAMES
f189ce4a74dd   flask-image   "python app.py"   3 seconds ago   Up 2 seconds   0.0.0.0:5000->5000/tcp, 5001/tcp   wonderful_buck

As you can see, your container has been started and expose on port 5000. You can also verify your container log with the following command:

docker container logs wonderful_buck

You should see the following output:

* Serving Flask app "app" (lazy loading)
* Environment: production
  WARNING: This is a development server. Do not use it in a production deployment.
  Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 119-276-539

Now, open your web browser and access your Flask application using the URL http://your-server-ip:5000. You should see your demo page on the following screen:

install Docker and create a Flask application on the Ubuntu server

Conclusion

In the above post, you learned how to install Docker and create a Flask application on the Ubuntu server. You also learned how to deploy Flask application with Docker using a Dockerfile. You can now start building your own application and deploy it in the Docker environment.

Also Read: How To Deploy A Node.js Application On Linux Server?

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here