Docker is a containerization software that enables us to package all the dependencies required to run an application and deploy it as one single package in a loosely isolated environment called a container. Docker provides an efficient way of sharing our application without worrying about installing the dependencies.
Have you ever ran into an issue like conflicting software versions when someone has shared an application with you or you cloned a GitHub repo and now it’s not working.
One popular solution to this problem is Dockerizing your project or application. Docker would work the same on any operating system.
There are many details that could be not so important for the practical implementation of docker.
We would have to mainly concentrate on 3 things:
- Docker file: A blueprint for building images
- Docker image: Image is a template for running containers
- Docker container: The container is the actual process where we have the package project
We would be looking into 2 scenarios fo building a docker file for:
- Simple script/program
- Web application with a flask(env)
Simple script/program:
To build a docker file we would require commands such as from, add, run, cmd. Now let us go over each of the commands:
FROM — This command is used to provide a base image
ADD/COPY — This command would add the program file to the container
RUN — this command would install additional dependencies or packages like NumPy, pandas
CMD — this command would specify the executable path of the program.
we would be demonstrating a linear regression python file.
FROM python:3.8
# specifies that the base image is python 3.8
ADD LinearRegression.py.
# add/ copy specifies to add the program file LinearRegression to the container current folder. The period, in the end, specifies the current folder.
RUN pip install NumPy sklearn
# this would install NumPy, sklearn packages to the container
CMD [ “python”, “./LinearRegression.py”]
# specifies the executable path of the program and running the python file in the container.
We are done with the docker file. we would like to build the image with the help of this file.
docker build -t linear-reg.
The above command would build a docker image. hear -t stands for docker tag. and providing the name of the docker image as “linear-reg” is optional but strongly recommended as we would be working with multiple docker images. The period, in the end, specifies the location of the image as the current directory.
Now having completed the image. we would like to start our container. to start the container by
docker run linear-reg
# docker run is the command and “linear-reg” is the name of the container.
by running this command our container executes the .py file and we can see the output in the terminal.
The above steps can be followed to execute any python file which doesn’t take any input parameters. If the program takes in input we would have to replace the docker run command by
docker run -t -i linear-reg
# where -I stands for interactive mode and -t would provide a sudo terminal to input the values for the file.
Web application with a flask(env):
We would be dockerizing a simple web application using flask.
This would be slightly complicated than the previous example as we have created a virtual environment and we would have to install many more dependencies. it would be tedious to include all dependencies in the docker file. so we will create a requirement.txt with the help of pip freeze and then using requirements.txt in the docker file for convenience.
(simpleflask) anurag_k@MacBook-Pro simpleflask % pip list
Package Version
— — — — — — — — — –
click 7.1.2
Flask 1.1.2
itsdangerous 1.1.0
Jinja2 2.11.2
MarkupSafe 1.1.1
pip 20.3.3
setuptools 51.0.0
Werkzeug 1.0.1
The following command would write all the required dependencies to the file.
pip freeze > requirements.txt
There would be a slight difference in the commands we run as we would have to include the host and the port required for the web application. we will have to enter the host in the python application for simplicity.
After setting up the docker file we would have to run the following commands.
docker build -t simpleflask.
in the docker run command, we will have to specify that -p 5000:5000 is open for traffic.
docker run -p 5000:5000 simpleflask
Conclusion
We conclude here. I wish you know how to dockerize a python file and can also dockerize your web application.
Docker makes it easier to package, containerize, and share your application without any hassle.