Dockerizing your Flask Application

Step 1. Building a minimal Flask application

app.py
$ python app.py
* Serving Flask app “app” (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 217–967–934

Step 2. Creating a requirements file

$ pip freeze > requirements.txt
Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1

Step 3. Creating a docker file for your Flask application

Dockerfile

Step 4. Building the docker image

docker build  -t ImageName:TagName dir
$ docker build -t simple_flask_app:1.0.0 .
Sending build context to Docker daemon 25.23MB
Step 1/6 : FROM python:3
— -> 2cc378c061f7
Step 2/6 : COPY . /app
— -> 21f39fecd2f3
Step 3/6 : WORKDIR /app
— -> Running in 542d6593f653
Removing intermediate container 542d6593f653
— -> a3290259dab7
Step 4/6 : RUN pip install -r requirements.txt
— -> Running in 8b0749fe5eda
Collecting Click==7.0 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Flask==1.0.2 (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting itsdangerous==1.1.0 (from -r requirements.txt (line 3))
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2==2.10 (from -r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 5))
Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Collecting Werkzeug==0.14.1 (from -r requirements.txt (line 6))
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Building wheels for collected packages: MarkupSafe
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status ‘done’
Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built MarkupSafe
Installing collected packages: Click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask
Successfully installed Click-7.0 Flask-1.0.2 Jinja2–2.10 MarkupSafe-1.0 Werkzeug-0.14.1 itsdangerous-1.1.0
Removing intermediate container 8b0749fe5eda
— -> 2a371a7b9a66
Step 5/6 : ENV PATH /app:$PATH
— -> Running in ae31f1287ea3
Removing intermediate container ae31f1287ea3
— -> 19447be8cf4c
Step 6/6 : CMD [ “python”,”app.py” ]
— -> Running in 8e19bcb2ea86
Removing intermediate container 8e19bcb2ea86
— -> 9b4eedc4fe5e
Successfully built 9b4eedc4fe5e
Successfully tagged simple_flask_app:1.0.0
dinesh@dinesh-Inspiron-5559:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
simple_flask_app 1.0.0 9b4eedc4fe5e 5 minutes ago 958MB

Step 5. Running the Docker container

$ docker run -p <local_computer_port>:<container_port> -t <container_name/container_id>
$ docker run -p 5000:5000 7dcceeaa748b
* Serving Flask app “app” (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 732–829–290

Step 6. To kill the Docker container

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93ff720163fa 7dcceeaa748b “python app.py” 27 minutes ago Up 27 minutes 0.0.0.0:5000->5000/tcp elegant_saha
$ docker kill <Container_ID>

--

--

Software Engineer at GogoAir

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store