Getting Started with Docker and Flask
Introduction
Docker 🎯 and Flask are great combinations for creating web applications and API's.
Let's briefly look into how you, yes you over there an absolute beginner can get started with Docker and Flask.
Without wasting your time let me make you know what Docker and Flask really mean then we dive deeper into how we can create them.
Docker is a platform for building, running and shipping applications
Flask is a Python web micro-framework built with a small core and easy-to-extend philosophy. Flask framework is flexible , lightweight and highly structured
Prerequisites
Programming fundamentals
Basic familiarity with Git 💩( clone, push, pull and e.tc).Just the basics nothing more 🌔
Setting up Your Computer 💻
The getting started guide on Docker has detailed instructions for setting up Docker on Mac, Linux and Windows.
In my case I use windows 10 as my OS .
Once you are done installing Docker, test your Docker installation by running the following:
Open up a terminal window and create a folder for your project:
C:\Users\odhia>mkdir my-intro
C:\Users\odhia>cd my-intro
Then I open it using visual studio code as my editor via the below command
C:\Users\odhia\my-intro>code .
Then inside the editor I created three files(my_app.py, Dockerfile and requirements)
see my repo here Repository
Inside my_app.py we have
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Flask with docker!'
inside Dockerfile write the below code
FFROM python:3.9-slim-buster
WORKDIR /Users/odhia/my_app
ENV MY_INTRO=my_app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
in requirement.txt type
flask
open your terminal and type
C:\Users\odhia\my-intro> docker image build -t my-intro .
this will take some time , wait till it finishes
Then run the image by typing the below command in your terminal
docker run -d -p 79:5000 my-intro
To show running container, type
docker ps
You will see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0580c6995b4b docker101tutorial:latest "/docker-entrypoint.…" 27 minutes ago Up 27 minutes 80/tcp angry_goodall
13f7ca8a0a5d docker101tutorial "/docker-entrypoint.…" 33 minutes ago Up 33 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp docker-tutorial
To build the image called "my-intro_webservices"
run the command
docker-compose up -d
then type
docker images
an output like the one below will be seen
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> cea83b5079af 52 minutes ago 125MB
my-intro latest 4693d6dfab69 52 minutes ago 125MB
docker101tutorial latest df160a26968e About an hour ago 28.3MB
alpine/git latest b8f176fa3f0d 3 months ago 25.1MB
The above result are the list of our images
If in any case we wanna do deletion we can use the command
docker rmi df1 -f
NOTE: df1 is first three letters of my image ID
Top comments (0)