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.
- 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.
- 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.
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.
Dockerfilewill be used to hold our docker code.
requirements.txtwill be used to hold the dependencies of our project.
main.pywill be used to hold contents of our program.
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
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.
you can then use the following command to run your flask 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.
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 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
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.