Getting Started with Docker & Flask
Tags: docker, flask, webdev, devops
Published: 2021-09-13 09:12:44

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 
- [Docker]( https://docs.docker.com/engine/install/ )
- [Flask]( https://phoenixnap.com/kb/install-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnpk3hp5omh4n0kukddm.png)

- The ```Dockerfile``` will be used to hold our docker code.

- The ```requirements.txt``` will be used to hold the dependencies of our project.

- The ```main.py``` will 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yim83hr7w2ggcnz64p1z.png)

We then create an instance of our app using the Flask class
![flask](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ica1mk6652bq2rbkew8.png)

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]
( https://www.pythontutorial.net/advanced-python/python-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5832i22qlk6hz1k04n0v.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdn0yipez3sr8xot1fjp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geazek3bsjt2y2zisss2.png)

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

```bash
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9wtdoh13qbn84klorsz.png)

> `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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7adhynwshia018q0h3hz.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxhotze5dyau6czsw2gh.png)

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](https://docs.docker.com/) or [flask](https://flask.palletsprojects.com/en/2.0.x/) you can follow the links to help you.

Good luck with you endeavors.