Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 27, 2021 09:15 am GMT

Getting started with Docker & Flask.

Introduction

Docker makes it easier, simpler and safer to build, deploy and manage applications in a docker container.
This article will help you get a detailed understanding of;

  • What is docker?
  • Why use Docker?
  • What is a docker image?
  • What is a docker container?
  • Dockerizing a flask application
What is Docker?

Docker is an open source containerization platform for developing, shipping and running applications.

Docker packages software into standardized units called containers. Containers have everything the software needs to run including libraries, system tools, code, and runtime.

More about what docker is

Why use Docker?
  • Responsive deployment and scaling.
  • Faster And Consistent Delivery Of Applications.
  • Automated container creation- with docker, one can automatically build a container based on application source code.
  • Container reuse- just like templates for building new containers, existing containers can be used as base images.
Docker image.

Image is a read-only template with instruction for creating containers.
Docker images can be considered as the blueprint of the entire application environment that you create.

Docker container.
  • A Docker container is a virtualized run-time environment where users can isolate applications from the underlying system.
  • Containers are compact, portable units in which you can start up an application quickly and easily.
  • Docker containers contains all the essential things required to run an application like code, runtime, system tools, system libraries, and settings.
Dockerizing a flask application
File structure and setup
\-- dockerExample    |-- app.py    |-- Dockerfile    |-- requirements.txt   \-- templates        |-- index.html

First I created a simple Flask application and added the following code to app.py.

from flask import Flask,render_templateapp=Flask(__name__)@app.route('/')def home():    return render_template('index.html')if __name__==('__main__'):    app.run(debug=True)

Check how to create a simple flask application

Add the following code to index.html

<!DOCTYPE html><html><head><title>Getting started with Docker</title></head><body><p>This is my Dockerfile</p></body>

Now we need to dockerize the flask application by creating a Dockerfile
Dockerfileis a text document that contains all the commands a user could call on the command line to assemble an image.

Add the following code to docker;

FROM python:3.9.6COPY . /docCOPY ./requirements.txt /doc/requirements.txtWORKDIR /docEXPOSE 5000:5000RUN pip install -r requirements.txtCMD ["python","app.py"]
  • FROM keyword is used to specify the base image to be used. We'll be making use of a python base image.
  • WORKDIR defines the main directory of operations.
  • EXPOSE informs Docker that the container listens on the specified network ports at runtime.
  • RUN is used to install the projects dependencies.
  • CMD provide defaults for an executing container.

Read more

We can now build our image with the docker build command as shown below;

docker image build -t docker_example .

Screenshot (119)

Once the build process is done, we can run the application with the docker run command as shown below;

docker run -p 5000:5000 docker_example

Screenshot (122)


Original Link: https://dev.to/phylis/getting-started-with-docker-flask-262p

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