1. Install Docker Install docker in WSL2 https://www.jb51.net/article/223179.htm Will report an error:
Go to download and install docker under windows 2. Write codeA web server is started using the Flask framework, and its only function is: if there is an environment variable "NAME" in the current environment, it prints it after "Hello", otherwise it prints "Hello world", and finally prints the hostname of the current environment import os from flask import Flask import socket from gevent import pywsgi app = Flask(__name__) @app.route('/') def hello(): html = "<h3>Hello {name}!</h3>" \ "<b>Hostname:</b> {hostname}<br/>" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname()) if __name__ == "__main__": server = pywsgi.WSGIServer(('0.0.0.0', 12345), app) server.serve_forever() Export dependency packages pip freeze >requirements.txt Flask==2.0.1 gevent==21.8.0 greenlet==1.1.1 itsdangerous==2.0.1 Jinja2==3.0.1 MarkupSafe==2.0.1 Werkzeug==2.0.1 zope.event==4.5.0 zope.interface==5.4.0 3. Write a Dockerfile# Use the official Python development image as the base image FROM python:3.8-slim # Change the working directory to /app WORKDIR /app #Copy all the contents in the current directory to /app ADD . /app # Use pip to install the dependencies required by this application # RUN pip install --trusted-host pypi.python.org -r requirements.txt RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt # Domestic sources are faster# Allow external access to the container's port 12345 EXPOSE 12345 # Set environment variable ENV NAME World # Set the container process to: python app.py, that is: the startup command of this Python application CMD ["python", "app.py"] # CMD implicitly includes ENTRYPOINT, /bin/sh -c In WSL: Let Docker create an image, add -t tag, automatically load Dockerfile, and execute the statements in it docker build -t helloworld . [+] Building 17.4s (10/10) FINISHED => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 757B 0.0s => [internal] load .dockerignore 0.1s => =>transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:3.8-slim 2.9s => [auth] library/python:pull token for registry-1.docker.io 0.0s => [1/4] FROM docker.io/library/python:3.8-slim@sha256:4dd66d1ccaddaa0587851cb92b365bf3090dccb41393c6f8b 0.0s => [internal] load build context 0.1s => => transferring context: 813B 0.0s => CACHED [2/4] WORKDIR /app 0.0s => [3/4] ADD ./app 0.1s => [4/4] RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt 13.6s => exporting to image 0.6s => => exporting layers 0.6s => => writing image sha256:390d32b9f7a20ccd347361bd31450807d3e63d052e334865cf8460968ffceff4 0.0s => => naming to docker.io/library/helloworld 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them View Mirror (k8s)PC:/mnt/d/gitcode/k8s$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE helloworld latest 390d32b9f7a2 About a minute ago 169MB Start the container docker run -p 4000:12345 helloworld Because CMD has been specified in the Dockerfile. Otherwise, you have to add the process startup command at the end View container startup (base) $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6e051d1af6b helloworld "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp upbeat_elion -p 4000:12345 tells Docker to map port 12345 in the container to port 4000 on the host machine. The purpose of this is to access port 4000 of the host machine to see the results returned by the application in the container. curl http://localhost:4000 # <h3>Hello World!</h3><b>Hostname:</b> dc1c1343e366<br/> Completed the development and testing of an application using containers 4. Upload the imageRegister docker hub, log in with docker login command docker tag helloworld kobe24o/helloworld:v0 kobe24o is the account name (image repository), helloworld is the image name, and v0 is the version number assigned by itself docker push kobe24o/helloworld:v0 (k8s) $ docker push kobe24o/helloworld:v0 The push refers to repository [docker.io/kobe24o/helloworld] 931022d457d6: Pushing [=================> ] 16.07MB/47.27MB c76dc68917fc: Pushed 047ca6dfe9ab: Pushed d82f4c466b47: Mounted from library/python 5aa75f4e55e7: Mounted from library/python 74d6903a940b: Mounted from library/python 2f9c2b8e82bd: Mounted from library/python ba5a5fe43301: Mounted from library/python 5. Modify the image(base) $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd3bf057cb09 helloworld "python app.py" 7 seconds ago Up 5 seconds 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp compassionate_carver (base) $ docker exec -it dd3bf057cb09 /bin/sh # pwd /app # touch newfile.txt # ls Dockerfile app.py newfile.txt requirements.txt # exit (base) $ docker commit dd3bf057cb09 kobe24o/helloworld:v1 sha256:ca8880f84040f9bdd7ef13763b9c64f8bd4a513a74bc2b095be06aae5b60268a The above operation adds a new file to the image and commits it to save. docker inspect --format '{{ .State.Pid}}' dd3bf057cb09 1763 # View the process ID PID of the running container By viewing the proc file of the host machine, you can see the files corresponding to all Namespaces of this process. root:/# ls -l /proc/{PID}/ns/ total 0 lrwxrwxrwx 1 root root 0 Sep 14 11:15 cgroup -> 'cgroup:[4026531835]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 ipc -> 'ipc:[4026532220]' lrwxrwxrwx 1 root root 0 Sep 14 09:49 mnt -> 'mnt:[4026532218]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 net -> 'net:[4026531992]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid -> 'pid:[4026532221]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid_for_children -> 'pid:[4026532221]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 user -> 'user:[4026531837]' lrwxrwxrwx 1 root root 0 Sep 14 11:15 uts -> 'uts:[4026532219]' A process can choose to join the namespace of a certain process, so as to "enter" the container where the process is located. This is exactly the implementation principle of Push to the hub (base) $ docker push kobe24o/helloworld:v1 The push refers to repository [docker.io/kobe24o/helloworld] dfee38b42dbb: Pushed 931022d457d6: Layer already exists c76dc68917fc: Layer already exists 047ca6dfe9ab: Layer already exists d82f4c466b47: Layer already exists 5aa75f4e55e7: Layer already exists 74d6903a940b: Layer already exists 2f9c2b8e82bd: Layer already exists ba5a5fe43301: Layer already exists v1: digest: sha256:7af7ff571ea9fd70d48abeaa2b38a1ed1c1a4e5a933b722d82af25d3e889f84e size: 2206 This is the end of this article about Docker deployment of web applications written in Python. For more relevant content about Docker deployment of Python web applications, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Description of meta viewport attribute in HTML web page
>>: CSS code to achieve 10 modern layouts
There are four main MySQL string interception fun...
Table of contents What is two-way data binding Im...
*******************Introduction to HTML language (...
background CVE-2021-21972 An unauthenticated comm...
difficulty Two mask creation of svg graphics Firs...
Table of contents 1. Introduction 2. Deployment E...
Preface I believe that everyone has had experienc...
Table of contents 1. Install axios 2. Use of axio...
byzhangxinxu from https://www.zhangxinxu.com/word...
Table of contents Preface How to encapsulate a To...
How to use if in Linux to determine whether a dir...
Table of contents Preface 1.notnull 2. unique 3. ...
Preview versions of Safari (Technology Preview 10...
Table of contents Overview Require URL of the app...
Table of contents 1. State Hook 1. Basic usage 2....