DEV Community

ChungWei Wei
ChungWei Wei

Posted on • Originally published at kmp.tw on

[Docker] Build Flask Service Docker Image

Env

All Component

  • ### Alpine Linux Image With Python 3.12 ( recommendation )
  • ### Python Package List
  • ### Source Code <!--more-->

Image

img01

Folder Tree

img02

Build Image

File dockerfile

~/demo/docker-img/flask (master*) » cat dockerfile

FROM python:alpine3.18
WORKDIR /api
ADD ./files/ /api
RUN pip install -r requirements.txt
CMD python main.py

Enter fullscreen mode Exit fullscreen mode

File requirements.txt

~/demo/docker-img/flask/files (master*) » cat requirements.txt
Click==8.1.7
Flask==3.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==3.0.0
requests==2.31.0
Enter fullscreen mode Exit fullscreen mode

File main.py

#!/usr/bin/python3
# -*- coding: utf8 -*-

'''
author         GordonWei
date           10/15/2023
comment        Test Flask Website
'''

'''
Just test
'''

from flask import Flask, request, render_template
import flask, json

app = flask.Flask(__name__)
#app.config["DEBUG"] = True

commit = []

@app.route('/', methods=['GET'])
def home():
  return "<h1>Nothing, Just A Index!</h1>"

app.run(host='0.0.0.0', port='8008')
Enter fullscreen mode Exit fullscreen mode

Build

docker build -t <your-img-name:tag> .
Enter fullscreen mode Exit fullscreen mode

Demo

~/demo/docker-img/flask (master*) » docker build -t flask-demo:0.4 .
[+] Building 8.8s (9/9) FINISHED                                                                           docker:desktop-linux
 => [internal] load .dockerignore                                                                                          0.0s
 => => transferring context: 2B                                                                                            0.0s
 => [internal] load build definition from dockerfile                                                                       0.0s
 => => transferring dockerfile: 148B                                                                                       0.0s
 => [internal] load metadata for docker.io/library/python:alpine3.18                                                       0.0s
 => [1/4] FROM docker.io/library/python:alpine3.18                                                                         0.0s
 => [internal] load build context                                                                                          0.0s
 => => transferring context: 1.97kB                                                                                        0.0s
 => [2/4] WORKDIR /api                                                                                                     0.0s
 => [3/4] ADD ./files/ /api                                                                                                0.0s
 => [4/4] RUN pip install -r requirements.txt                                                                              8.6s
 => exporting to image                                                                                                     0.1s
 => => exporting layers                                                                                                    0.1s
 => => writing image sha256:e457f32666126e35eaca9bbe4aa55accc591a2ebfec2386203889262cbdce9cf                               0.0s
 => => naming to docker.io/library/flask-demo:0.4                                                                          0.0s

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview
Enter fullscreen mode Exit fullscreen mode

Verify

~/demo/docker-img/flask (master*) » docker images | grep flask-demo
flask-demo                                              0.4               e457f3266612   About a minute ago   76.6MB
flask-demo                                              0.3               50d74e0a22b0   2 days ago           76.6MB
Enter fullscreen mode Exit fullscreen mode

Try Run

~/demo/docker-img/flask (master*) » docker run -idt -p 80:8008 flask-demo:0.4
3d886093528c70d7aed6c037674f059d83260b60a689ba633c342a162f430380
Enter fullscreen mode Exit fullscreen mode

Screenshot

img03

Top comments (0)