Cannot we isolate our apps so that they are independent of each other and run perfectly ? The answer is absolutely “YES”, that correct that’s very much possible with docker and containers. They provide you isolated environment and are your friend for deploying many applications with each taking its own container. You can run as many as containers in docker and are independent of each other. They all share same kernel memory.
In this tutorial we will go through a simple demonstration of a python application which will run on docker engine.
Table of content
- What is Python ?
- What is docker ?
- Create a Python flask application
- Create a Docker file
- Build Docker Image
- Run the Python flask application Container
What is Python ?
Python is a language from which you create web applications and system scripts. It is a used vastly across the organizations and very easy to learn. Python apps require isolated environment to run its application very well. This is quite possible with Docker and containers which we will use in this tutorial.
If you wish to know more about python please visit our Python’s Page to learn all about Python.
What is docker ?
Docker is an open source tool for developing , shipping and running applications. It has ability to run applications in loosely isolated environment using containers. Docker is an application which helps in management of containers in a very smooth and effective way. In containers you can isolate your applications. Docker is quite similar to virtual machine but it is light weighted and can be ported easily.
Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines ie. hosts kernel.
- Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
- Recommended to have 4GB RAM
- At least 5GB of drive space
- Docker installed on ubuntu machine. Please visit our another blog on how-to-setup-docker-step-by-step-on-ubuntu-machine
You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.
Create a Python flask application
- Before we create our first program using python flask we need to install python flask and python virtual environment for flask to run.
pip install virtualenv # virtual python environment
- Create and activate a virtual environment named virt:
virtualenv venv source virt/bin/activate
- Finally install Flask
pip install flask # Install Flask from pip
- Now create a text file and name it as app.py where we will write our first python flask code as below.
from flask import Flask # Importing the class flask app = Flask(__name__) # Creating the Flask class object. @app.route('/') # app.route informs flask about the URL to be used by function def func(): # Creating a function return("Iam from Automateinfra.com") if __name__ == "__main__": # Programs starts from here. app.run(debug=True)
- Create one more file in same directory and name it as requirements.txt where we will define the dependency of flask application
- Now our python code app.py and requirements.txt are ready for execution. Lets execute our code using below command.
- Great, so our python flask application ran successfully on our local machine. Now we need to execute same code on docker . Lets now move to docker part.
Create a docker file
Docker file is used to create a customized docker images on top of basic docker image. It is a text file that contains all the commands to build or assemble a new docker image. Using docker
build command we can create new customized docker images . Its basically another layer which sits on top of docker image. Using newly built docker image we can run containers in similar way.
- Create a docker file and name it as Docker file . Keep this file also in same directory as app.py and requirements.txt
FROM python:3.8 # Sets the base image WORKDIR /code # Sets the working directory in the container COPY requirements.txt . # copy the dependencies file to the working directory RUN pip install -r requirements.txt # Install dependencies COPY src/ . # Copy the content of the local src directory to the working directory CMD [ "python", "./app.py" ] # Command to run on container start
Build docker Image
- Now we are ready to build our new image . So lets build our image
docker build -t myimage .
- You should see the docker images by now.
Run the Python flask application Container
- Now run our first container using same docker image ( myimage)
docker run -d -p 5000:5000 myimage
- Verify if container is successfully created.
docker ps -a
In this tutorial we covered what is docker , what is python and using python flask application created a application on docker engine in one of the containers.
Hope this tutorial will helps you in understanding and setting up Python flask and python flask applications on docker engine in ubuntu machine.
Please share with your friends.