{"id":1094,"date":"2021-04-08T17:29:15","date_gmt":"2021-04-08T10:29:15","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=1094"},"modified":"2021-12-29T18:29:12","modified_gmt":"2021-12-29T11:29:12","slug":"chay-flask-python-tren-docker","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/chay-flask-python-tren-docker.html","title":{"rendered":"2 b\u01b0\u1edbc \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 ch\u1ea1y m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh Flask Python tr\u00ean docker"},"content":{"rendered":"\r\n<div style=\"text-align: justify;\">\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Flask cho ph\u00e9p b\u1ea1n x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng web t\u1eeb \u0111\u01a1n gi\u1ea3n t\u1edbi ph\u1ee9c t\u1ea1p. N\u00f3 c\u00f3 th\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c api nh\u1ecf, \u1ee9ng d\u1ee5ng web ch\u1eb3ng h\u1ea1n nh\u01b0 c\u00e1c trang web, blog, trang wiki ho\u1eb7c m\u1ed9t website d\u1ef1a theo th\u1eddi gian hay th\u1eadm ch\u00ed l\u00e0 m\u1ed9t trang web th\u01b0\u01a1ng m\u1ea1i. Flask cung c\u1ea5p cho b\u1ea1n c\u00f4ng c\u1ee5, c\u00e1c th\u01b0 vi\u1ec7n v\u00e0 c\u00e1c c\u00f4ng ngh\u1ec7 h\u1ed7 tr\u1ee3 b\u1ea1n l\u00e0m nh\u1eefng c\u00f4ng vi\u1ec7c tr\u00ean.<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">L\u00e0 m\u1ed9t Micro-framework n\u00ean Flask r\u1ea5t g\u1ecdn nh\u1eb9, c\u00f3 t\u00ednh \u0111\u1ed9c l\u1eadp v\u00e0 b\u1ea3o m\u1eadt cao do \u00edt ph\u1ee5 thu\u1ed9c v\u00e0o c\u00e1c th\u01b0 vi\u1ec7n b\u00ean ngo\u00e0i. \u0110\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n, ch\u00fang ta c\u00f9ng nhau c\u00e0i \u0111\u1eb7t Flask v\u00e0 t\u1ea1o th\u1eed \u1ee9ng d\u1ee5ng \u0111\u1ea7u ti\u00ean Hello world nh\u00e9. <\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">\u1ede \u0111\u00e2y m\u00ecnh s\u1ebd d\u00f9ng <a href=\"https:\/\/www.marketenterprise.vn\/blog\/docker-la-gi.html\">Docker<\/a> \u0111\u1ec3 x\u00e2y d\u1ef1ng m\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n Python \u0111\u1ec3 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng t\u1edbi m\u00f4i tr\u01b0\u1eddng th\u1eadt tr\u00ean m\u00e1y t\u00ednh c\u1ee7a m\u00ecnh v\u00e0 c\u00e0i Flask tr\u00ean m\u00f4i tr\u01b0\u1eddng n\u00e0y.<\/span><\/span><\/span><\/p>\r\n<h2 class=\"cjk\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\">B\u01b0\u1edbc 1: T\u1ea1o m\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n Python 3 v\u00e0 c\u00e0i \u0111\u1eb7t Flask<\/span><\/span><\/h2>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Tr\u01b0\u1edbc ti\u00ean ch\u00fang ta t\u1ea1o m\u1ed9t th\u01b0 m\u1ee5c ch\u1ee9a project c\u1ee7a m\u00ecnh, \u1edf \u0111\u00e2y m\u00ecnh s\u1ebd \u0111\u1eb7t t\u00ean cho th\u01b0 m\u1ee5c n\u00e0y l\u00e0 demo-flask.<\/span><\/span><\/span><\/p>\r\n<p><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">T\u1ea1o file Dockerfile trong th\u01b0 m\u1ee5c docker\/python v\u1edbi n\u1ed9i dung sau:<\/span><\/span><\/span><\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\" data-enlighter-theme=\"monokai\">FROM python:3.9.2-alpine\r\nWORKDIR \/home\/app\r\nRUN apk add --no-cache gcc musl-dev linux-headers\r\nRUN pip install Flask\r\nENV FLASK_APP=app.py\r\nENV FLASK_RUN_HOST=0.0.0.0\r\nENV FLASK_ENV=development\r\nEXPOSE 5000\r\nCOPY . .\r\nCMD [\"flask\", \"run\"]<\/pre>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\"><span style=\"text-align: justify;\">FROM: Khai b\u00e1o image Python v\u00e0 phi\u00ean b\u1ea3n.<\/span>Trong \u0111\u00f3 ch\u00fa \u00fd \u0111\u1ebfn:<\/span><\/span><\/span><\/p>\r\n<ul>\r\n<li>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">WORKDIR: Khai b\u00e1o th\u01b0 m\u1ee5c l\u00e0m vi\u1ec7c m\u1eb7c \u0111\u1ecbnh.<\/span><\/span><\/span><\/p>\r\n<\/li>\r\n<li>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">RUN: D\u00f9ng \u0111\u1ec3 ch\u1ea1y l\u1ec7nh command.<\/span><\/span><\/span><\/p>\r\n<\/li>\r\n<li>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">ENV: D\u00f9ng khai b\u00e1o bi\u1ebfn m\u00f4i tr\u01b0\u1eddng Python cho Flask.<\/span><\/span><\/span><\/p>\r\n<\/li>\r\n<li>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Flask s\u1ebd \u0111\u01b0\u1ee3c c\u00e0i trong Python qua l\u1ec7nh <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">pip install Flask<\/code><\/span><\/span><\/span><\/p>\r\n<\/li>\r\n<\/ul>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">T\u1ea1o file docker-compose.yml trong th\u01b0 m\u1ee5c g\u1ed1c project v\u1edbi n\u1ed9i dung sau:<\/span><\/span><\/span><\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\" data-enlighter-theme=\"monokai\">version: \"3.9\"\r\nservices:\r\n  python:\r\n    container_name: \"SV-Python3-Flask\"\r\n    build: .\/docker\/python\r\n    ports:\r\n      - \"5000:5000\"\r\n    volumes:\r\n      - $PWD\/backend:\/home\/app<\/pre>\r\n<ul>\r\n<li><span style=\"font-family: Arial, serif;\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Ch\u1ea1y l\u1ec7nh <code class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\" data-enlighter-theme=\"monokai\">docker-compose up -d<\/code><\/span><\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">\u00a0\u0111\u1ec3 kh\u1edfi t\u1ea1o container.<\/span><\/span><\/span><\/li>\r\n<li><span style=\"font-family: Arial, serif;\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Ch\u1ea1y l\u1ec7nh <code class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\" data-enlighter-theme=\"monokai\">docker exec -it SV-Python3-Flask sh<\/code>\u00a0<\/span><\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">\u00a0\u0111\u1ec3 truy c\u1eadp v\u00e0o container.<\/span><\/span><\/span><\/li>\r\n<li><span style=\"font-family: Arial, serif;\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Ch\u1ea1y l\u1ec7nh <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"monokai\">flask --version<\/code>\u00a0<\/span><\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">\u00a0\u0111\u1ec3 ki\u1ec3m tra th\u1eed Flask \u0111\u00e3 c\u00e0i th\u00e0nh c\u00f4ng hay ch\u01b0a. <\/span><\/span><\/span><\/li>\r\n<li><span style=\"font-family: Arial, serif;\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">N\u1ebfu ch\u01b0a th\u00ec c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng command sau \u0111\u00e2y \u0111\u1ec3 c\u00e0i Flask: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"monokai\">pip install Flask<\/code>\u00a0<\/span><\/span><\/span><\/span><\/li>\r\n<\/ul>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Ngo\u00e0i c\u00e1ch tr\u00ean, b\u1ea1n c\u0169ng c\u00f3 th\u1ec3 tham kh\u1ea3o qua c\u00e1c c\u00e1ch c\u00e0i \u0111\u1eb7t kh\u00e1c t\u1eeb t\u00e1c gi\u1ea3 <\/span><a href=\"https:\/\/flask.palletsprojects.com\/en\/master\/installation\/\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #1155cc;\"><span style=\"font-size: small;\"><u>t\u1ea1i \u0111\u00e2y<\/u><\/span><\/span><\/a><\/span><\/span><\/p>\r\n<h2 class=\"cjk\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\">B\u01b0\u1edbc 2: Kh\u1edfi t\u1ea1o ch\u01b0\u01a1ng tr\u00ecnh Hello world<\/span><\/span><\/h2>\r\n<p class=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Sau khi c\u00e0i xong m\u00f4i tr\u01b0\u1eddng, ch\u00fang ta b\u1eaft \u0111\u1ea7u t\u1ea1o \u1ee9ng d\u1ee5ng Hello world. Nh\u01b0 \u1edf ph\u1ea7n 1, l\u00fac t\u1ea1o Dockerfile ch\u00fang ta \u0111\u00e3 khai b\u00e1o c\u00e1c bi\u1ebfn m\u00f4i tr\u01b0\u1eddng cho \u1ee9ng d\u1ee5ng Flask. Trong \u0111\u00f3 c\u00f3 khai b\u00e1o file ch\u1ea1y m\u1eb7c \u0111\u1ecbnh l\u00e0 app.py t\u1ea1i d\u00f2ng <code class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\">ENV FLASK_APP=app.py<\/code><\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">. V\u00ec v\u1eady ch\u00fang ta h\u00e3y t\u1ea1o file app.py trong th\u01b0 m\u1ee5c l\u00e0m vi\u1ec7c \/app v\u00e0 code v\u00e0o \u0111\u00f3.<\/span><\/span><\/span><\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"monokai\">from flask import Flask\r\napp = Flask(__name__)\r\n \r\n@app.route('\/')\r\ndef hello_world():\r\n    return 'Hello, World!'\r\n \r\nif __name__ == '__main__':\r\n    app.run()<\/pre>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">M\u00ecnh s\u1ebd gi\u1ea3i th\u00edch t\u1eebng d\u00f2ng code nh\u01b0 sau:<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">D\u00f2ng 1: Th\u1ef1c hi\u1ec7n import th\u01b0 vi\u1ec7n Flask. C\u1ee5 th\u1ec3 \u1edf \u0111\u00e2y ch\u00fang ta import class Flask. \u0110\u00e2y l\u00e0 vi\u1ec7c c\u1ea7n v\u00e0 ph\u1ea3i l\u00e0m cho m\u1ed7i \u1ee9ng d\u1ee5ng s\u1eed d\u1ee5ng Flask.<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">D\u00f2ng 2: Khai b\u00e1o 1 th\u1ec3 hi\u1ec7n c\u1ee7a class Flask. N\u00f3 s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng xuy\u00ean su\u1ed1t trong ch\u01b0\u01a1ng tr\u00ecnh c\u1ee7a ch\u00fang ta.<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">D\u00f2ng 3: \u0110\u1ecbnh tuy\u1ebfn URL truy c\u1eadp th\u1ef1c thi h\u00e0m hello_world() \u1edf d\u00f2ng 4.<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">D\u00f2ng <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">if __name__ == '__main__':<\/code>: <\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">H\u00e0m main c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh, trong h\u00e0m n\u00e0y ch\u00fang ta th\u00eam d\u00f2ng code <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"monokai\">app.run() <\/code><\/span><\/span><\/span><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">\u0111\u1ec3 ch\u01b0\u01a1ng tr\u00ecnh ch\u00fang ta l\u1eafng nghe c\u00e1c request t\u1eeb user.<\/span><\/span><\/span><\/p>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Sau khi t\u1ea1o xong file ch\u01b0\u01a1ng tr\u00ecnh, ch\u00fang ta m\u1edf link localhost:5000 \u0111\u1ec3 xem k\u1ebft qu\u1ea3.<\/span><\/span><\/span><\/p>\r\n<h2 align=\"left\">T\u1ed5ng k\u1ebft<\/h2>\r\n<p align=\"left\"><span style=\"font-family: Arial, serif;\"><span lang=\"vi\"><span style=\"font-size: small;\">Nh\u01b0 v\u1eady, ch\u00fang ta v\u1eeba t\u00ecm hi\u1ec3u xong th\u01b0 vi\u1ec7n Flask Python l\u00e0 g\u00ec? Hi v\u1ecdng r\u1eb1ng b\u00e0i vi\u1ebft \u0111em l\u1ea1i \u0111\u01b0\u1ee3c cho b\u1ea1n c\u00e1i nh\u00ecn t\u1ed5ng quan v\u1ec1 micro-framework Flask. Trong b\u00e0i vi\u1ebft n\u00e0y, m\u00ecnh ch\u1ee7 y\u1ebfu gi\u1edbi thi\u1ec7u v\u1ec1 Flask. N\u1ebfu b\u1ea1n h\u1ee9ng th\u00fa th\u00ec b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u th\u00eam <\/span><a href=\"https:\/\/flask.palletsprojects.com\/en\/1.1.x\/quickstart\/\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #1155cc;\"><span style=\"font-size: small;\"><u>t\u1ea1i \u0111\u00e2y<\/u><\/span><\/span><\/a><span style=\"font-size: small;\">. Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng.<\/span><\/span><\/span><\/p>\r\n<\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>Flask l\u00e0 m\u1ed9t Framework web c\u1ee7a Python nh\u01b0ng l\u00e0 m\u1ed9t Micro-framework b\u1edfi n\u00f3 ch\u1ec9 c\u00f3 ph\u1ea7n khung c\u01a1 b\u1ea3n, c\u00e1c ph\u1ea7n c\u00f2n l\u1ea1i cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng t\u00f9y ch\u1ecdn m\u00e0 \u0111\u1eafp v\u00e0o, kh\u00f4ng y\u00eau c\u1ea7u c\u00e1c c\u00f4ng c\u1ee5 ho\u1eb7c th\u01b0 vi\u1ec7n c\u1ee5 th\u1ec3. \u0110\u01b0\u1ee3c ph\u00e1t tri\u1ec3n b\u1edfi Armin Ronacher, ng\u01b0\u1eddi d\u1eabn \u0111\u1ea7u Pocco &#8211; m\u1ed9t nh\u00f3m nh\u1eefng ng\u01b0\u1eddi \u0111am m\u00ea Python qu\u1ed1c t\u1ebf. Flask ph\u00e1t tri\u1ec3n d\u1ef1a tr\u00ean b\u1ed9 c\u00f4ng c\u1ee5 Werkzeug WSGI, Jinja2\u00a0 v\u00e0 c\u1ea3 hai \u0111\u1ec1u l\u00e0 c\u00e1c d\u1ef1 \u00e1n c\u1ee7a Pocco.<\/p>\n","protected":false},"author":31,"featured_media":1840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/1840"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}