Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 13, 2021 09:12 am GMT

Getting Started with Docker & Flask

Probably your wondering what docker is all about, or your just heard it from you friends. Worry no more cause you've come to the right post.

What is docker ?

  • Docker is a containerization software used to bundle your project into an executable source component shipping with dependencies and operating system libraries that enable the source code to run.
  • In simple terms docker is a revolutionary software that enable one to bundle his software and enable the programmer run his program in any other computer without pre-installing necessary libraries every time.

What is Flask ?

  • Flask is a python framework used for creating web application. Why flask? Flask is know for it fast nature and also it is more flexible when you want no restriction in terms of coding paradigm. Its also easy to learn as compared to Django.

Getting Started

Use the following links to install both docker and flask

After successful installation, we are going to create a simple flask project that handles both GET and POST.

Create an empty directory named src
inside this directory is where we are going to place our program.

Create and three files like so
Alt Text

  • TheDockerfilewill be used to hold our docker code.
  • Therequirements.txtwill be used to hold the dependencies of our project.
  • Themain.pywill be used to hold contents of our program.

Working with Flask

In order to first get started with Flask we first import the class Flask from the module flask
importing flask

We then create an instance of our app using the Flask class
flask

Using the apps' instance we can then create route ( Endpoints ) for our web application. The app is used as a decorator in this instance where we provide:
the name of the endpoint and the method for the endpoint.
Learn more about decorators

Since this is an introduction course we are going to build a simple hello world application.
For a simple web page to display hello world in the page we can add the following
python decorators

In case you want your endpoint to be used for more than a GET request we can easily add the methods to the method list like so
python decorators

To run our application well have to call run on the apps' instance. We can optionally provide arguments like:

  • debug - Tells the app to run in debug or production mode
  • host - defines the host of the application
  • port - defines the port number for the application to run in.

If the arguments are not provided then it will use the default values.
flask run

you can then use the following command to run your flask application

python main.py

Containerizing our Application

Now that we have our miniature flask application now we can containerize it using docker.
Moving to our Dockerfile we can first add the following code.

docker code

FROM - defined the base image or the operations
COPY - copies files and folders from the source to the destination filesystem path
WORKDIR - sets the working directory of the destination filesystem
RUN - runs the commands on top of the current image
EXPOSE - define the network port for our container to listen to during runtime
CMD - defines the command for executing our

We then have to build our application so that it may setup the environment and also download the necessary file for it to run.
docker run

We specify the container name. We have names ours getting-started and also the path of our application.

Since we exposed our application to run on port 5000. We can then run our application like so

docker

our application with the run on port 5000.
Our app will be bundled into a container which you can easily share with your fellow programmers.
Hope this article was insightful and you were able to grasp something about docker and flask. If you need a deep dive into docker or flask you can follow the links to help you.

Good luck with you endeavors.


Original Link: https://dev.to/machel/getting-started-with-docker-flask-5bm7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To