From aa2059a38c32fde5a738388f1b03f951ad863105 Mon Sep 17 00:00:00 2001
From: barney <15270405776@163.com>
Date: Mon, 20 Feb 2023 18:29:42 +0800
Subject: [PATCH] docker_tutorial
---
{python => 1-intro}/.dockerignore | 2 +-
1-intro/Dockerfile | 8 ++++++++
1-intro/hello.py | 1 +
2-common-commands/pyramid/Dockerfile | 4 ++++
2-common-commands/pyramid/pyramid.py | 11 +++++++++++
2-common-commands/volume/Dockerfile | 6 ++++++
2-common-commands/volume/requirements.txt | 2 ++
2-common-commands/volume/server.py | 17 +++++++++++++++++
2-common-commands/volume/templates/index.html | 17 +++++++++++++++++
2-common-commands/volume/volume_data/book.json | 3 +++
2-common-commands/webapp/backend/Dockerfile | 6 ++++++
2-common-commands/webapp/backend/book.json | 5 +++++
.../webapp/backend/requirements.txt | 2 ++
2-common-commands/webapp/backend/server.py | 15 +++++++++++++++
2-common-commands/webapp/frontend/Dockerfile | 6 ++++++
.../webapp/frontend/requirements.txt | 1 +
2-common-commands/webapp/frontend/server.py | 10 ++++++++++
.../webapp/frontend/templates/index.html | 16 ++++++++++++++++
3-docker-compose/app1/backend/Dockerfile | 6 ++++++
3-docker-compose/app1/backend/book.json | 5 +++++
3-docker-compose/app1/backend/requirements.txt | 2 ++
3-docker-compose/app1/backend/server.py | 15 +++++++++++++++
3-docker-compose/app1/docker-compose.yml | 15 +++++++++++++++
3-docker-compose/app1/frontend/Dockerfile | 6 ++++++
3-docker-compose/app1/frontend/requirements.txt | 1 +
3-docker-compose/app1/frontend/server.py | 10 ++++++++++
.../app1/frontend/templates/index.html | 16 ++++++++++++++++
3-docker-compose/app2/backend/Dockerfile | 6 ++++++
3-docker-compose/app2/backend/requirements.txt | 2 ++
3-docker-compose/app2/backend/server.py | 16 ++++++++++++++++
3-docker-compose/app2/docker-compose.yml | 14 ++++++++++++++
README.md | 2 +-
docker_tutorial.code-workspace | 8 --------
python/Dockerfile | 8 --------
python/hello.py | 1 -
35 files changed, 246 insertions(+), 19 deletions(-)
rename {python => 1-intro}/.dockerignore (75%)
create mode 100644 1-intro/Dockerfile
create mode 100644 1-intro/hello.py
create mode 100644 2-common-commands/pyramid/Dockerfile
create mode 100644 2-common-commands/pyramid/pyramid.py
create mode 100644 2-common-commands/volume/Dockerfile
create mode 100644 2-common-commands/volume/requirements.txt
create mode 100644 2-common-commands/volume/server.py
create mode 100644 2-common-commands/volume/templates/index.html
create mode 100644 2-common-commands/volume/volume_data/book.json
create mode 100644 2-common-commands/webapp/backend/Dockerfile
create mode 100644 2-common-commands/webapp/backend/book.json
create mode 100644 2-common-commands/webapp/backend/requirements.txt
create mode 100644 2-common-commands/webapp/backend/server.py
create mode 100644 2-common-commands/webapp/frontend/Dockerfile
create mode 100644 2-common-commands/webapp/frontend/requirements.txt
create mode 100644 2-common-commands/webapp/frontend/server.py
create mode 100644 2-common-commands/webapp/frontend/templates/index.html
create mode 100644 3-docker-compose/app1/backend/Dockerfile
create mode 100644 3-docker-compose/app1/backend/book.json
create mode 100644 3-docker-compose/app1/backend/requirements.txt
create mode 100644 3-docker-compose/app1/backend/server.py
create mode 100644 3-docker-compose/app1/docker-compose.yml
create mode 100644 3-docker-compose/app1/frontend/Dockerfile
create mode 100644 3-docker-compose/app1/frontend/requirements.txt
create mode 100644 3-docker-compose/app1/frontend/server.py
create mode 100644 3-docker-compose/app1/frontend/templates/index.html
create mode 100644 3-docker-compose/app2/backend/Dockerfile
create mode 100644 3-docker-compose/app2/backend/requirements.txt
create mode 100644 3-docker-compose/app2/backend/server.py
create mode 100644 3-docker-compose/app2/docker-compose.yml
delete mode 100644 docker_tutorial.code-workspace
delete mode 100644 python/Dockerfile
delete mode 100644 python/hello.py
diff --git a/python/.dockerignore b/1-intro/.dockerignore
similarity index 75%
rename from python/.dockerignore
rename to 1-intro/.dockerignore
index 96571bf..9072039 100644
--- a/python/.dockerignore
+++ b/1-intro/.dockerignore
@@ -1,2 +1,2 @@
__pycache__
-env
\ No newline at end of file
+env
diff --git a/1-intro/Dockerfile b/1-intro/Dockerfile
new file mode 100644
index 0000000..9f823cb
--- /dev/null
+++ b/1-intro/Dockerfile
@@ -0,0 +1,8 @@
+# 将 image 文件继承与官方的3.7版本的Python
+FROM python:3.7
+# 将当前目录下的所有文件(除了 .dockerignore 排除的路径),都拷贝进 image 文件的 /app 目录。
+COPY . /app
+# 指定接下来的工作路径为 /app
+WORKDIR /app
+# 在 /app 目录下,运行 python 文件
+CMD python3 hello.py
diff --git a/1-intro/hello.py b/1-intro/hello.py
new file mode 100644
index 0000000..df1dc68
--- /dev/null
+++ b/1-intro/hello.py
@@ -0,0 +1 @@
+print('Hello World')
diff --git a/2-common-commands/pyramid/Dockerfile b/2-common-commands/pyramid/Dockerfile
new file mode 100644
index 0000000..f150916
--- /dev/null
+++ b/2-common-commands/pyramid/Dockerfile
@@ -0,0 +1,4 @@
+FROM python:3.7
+COPY ./pyramid.py /pyramid.py
+ENTRYPOINT ["python3", "pyramid.py"]
+CMD ["5"] # 指定参数时会被覆盖
diff --git a/2-common-commands/pyramid/pyramid.py b/2-common-commands/pyramid/pyramid.py
new file mode 100644
index 0000000..08af286
--- /dev/null
+++ b/2-common-commands/pyramid/pyramid.py
@@ -0,0 +1,11 @@
+import sys
+
+def print_pyramid(level: int):
+ for i in range(level):
+ new_line_str = ''
+ new_line_str += ' ' * (level - 1 - i)
+ new_line_str += '*' * ((i+1) * 2 - 1)
+ print(new_line_str)
+
+input_level = int(sys.argv[1])
+print_pyramid(input_level)
diff --git a/2-common-commands/volume/Dockerfile b/2-common-commands/volume/Dockerfile
new file mode 100644
index 0000000..2e9eb8c
--- /dev/null
+++ b/2-common-commands/volume/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 3000
+CMD ["python3", "server.py"]
diff --git a/2-common-commands/volume/requirements.txt b/2-common-commands/volume/requirements.txt
new file mode 100644
index 0000000..1bff53d
--- /dev/null
+++ b/2-common-commands/volume/requirements.txt
@@ -0,0 +1,2 @@
+flask==2.1.0
+flask-cors==3.0.8
diff --git a/2-common-commands/volume/server.py b/2-common-commands/volume/server.py
new file mode 100644
index 0000000..fea88cc
--- /dev/null
+++ b/2-common-commands/volume/server.py
@@ -0,0 +1,17 @@
+from flask import Flask, render_template
+import json
+
+app = Flask(__name__)
+
+@app.route('/book')
+def json_file():
+ file = open('volume_data/book.json')
+ json_data = json.load(file)
+ return json_data
+
+@app.route('/')
+def home():
+ return render_template('index.html')
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=3000)
diff --git a/2-common-commands/volume/templates/index.html b/2-common-commands/volume/templates/index.html
new file mode 100644
index 0000000..65e434d
--- /dev/null
+++ b/2-common-commands/volume/templates/index.html
@@ -0,0 +1,17 @@
+
+
+
+ Sample Book Info
+
+
+ Volume Sample Book
+
+
+
+
diff --git a/2-common-commands/volume/volume_data/book.json b/2-common-commands/volume/volume_data/book.json
new file mode 100644
index 0000000..97b8528
--- /dev/null
+++ b/2-common-commands/volume/volume_data/book.json
@@ -0,0 +1,3 @@
+{
+ "title": "The Book Title"
+}
diff --git a/2-common-commands/webapp/backend/Dockerfile b/2-common-commands/webapp/backend/Dockerfile
new file mode 100644
index 0000000..09ee055
--- /dev/null
+++ b/2-common-commands/webapp/backend/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 5000
+CMD ["python3", "server.py"]
diff --git a/2-common-commands/webapp/backend/book.json b/2-common-commands/webapp/backend/book.json
new file mode 100644
index 0000000..dd852f1
--- /dev/null
+++ b/2-common-commands/webapp/backend/book.json
@@ -0,0 +1,5 @@
+{
+ "title": "Book Title",
+ "description": "Description",
+ "link": "turingplanet.org"
+}
\ No newline at end of file
diff --git a/2-common-commands/webapp/backend/requirements.txt b/2-common-commands/webapp/backend/requirements.txt
new file mode 100644
index 0000000..1bff53d
--- /dev/null
+++ b/2-common-commands/webapp/backend/requirements.txt
@@ -0,0 +1,2 @@
+flask==2.1.0
+flask-cors==3.0.8
diff --git a/2-common-commands/webapp/backend/server.py b/2-common-commands/webapp/backend/server.py
new file mode 100644
index 0000000..5765da9
--- /dev/null
+++ b/2-common-commands/webapp/backend/server.py
@@ -0,0 +1,15 @@
+from flask import Flask, render_template
+from flask_cors import CORS # need to mention
+import json
+
+app = Flask(__name__)
+CORS(app)
+
+@app.route('/book')
+def json_file():
+ file = open('./book.json')
+ json_data = json.load(file)
+ return json_data
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=5000)
diff --git a/2-common-commands/webapp/frontend/Dockerfile b/2-common-commands/webapp/frontend/Dockerfile
new file mode 100644
index 0000000..eb4693c
--- /dev/null
+++ b/2-common-commands/webapp/frontend/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 3000
+ENTRYPOINT ["python3", "server.py"]
diff --git a/2-common-commands/webapp/frontend/requirements.txt b/2-common-commands/webapp/frontend/requirements.txt
new file mode 100644
index 0000000..aba55f1
--- /dev/null
+++ b/2-common-commands/webapp/frontend/requirements.txt
@@ -0,0 +1 @@
+flask==2.1.0
diff --git a/2-common-commands/webapp/frontend/server.py b/2-common-commands/webapp/frontend/server.py
new file mode 100644
index 0000000..52a1681
--- /dev/null
+++ b/2-common-commands/webapp/frontend/server.py
@@ -0,0 +1,10 @@
+from flask import Flask, render_template
+
+app = Flask(__name__)
+
+@app.route('/')
+def home():
+ return render_template('index.html')
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=3000)
diff --git a/2-common-commands/webapp/frontend/templates/index.html b/2-common-commands/webapp/frontend/templates/index.html
new file mode 100644
index 0000000..2314486
--- /dev/null
+++ b/2-common-commands/webapp/frontend/templates/index.html
@@ -0,0 +1,16 @@
+
+
+
+ Sample Book Info
+
+
+ Web App Sample Book
+
+
+
+
diff --git a/3-docker-compose/app1/backend/Dockerfile b/3-docker-compose/app1/backend/Dockerfile
new file mode 100644
index 0000000..09ee055
--- /dev/null
+++ b/3-docker-compose/app1/backend/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 5000
+CMD ["python3", "server.py"]
diff --git a/3-docker-compose/app1/backend/book.json b/3-docker-compose/app1/backend/book.json
new file mode 100644
index 0000000..f658cbd
--- /dev/null
+++ b/3-docker-compose/app1/backend/book.json
@@ -0,0 +1,5 @@
+{
+ "title": "Title_1",
+ "description": "Description",
+ "link": "turingplanet.org"
+}
diff --git a/3-docker-compose/app1/backend/requirements.txt b/3-docker-compose/app1/backend/requirements.txt
new file mode 100644
index 0000000..1bff53d
--- /dev/null
+++ b/3-docker-compose/app1/backend/requirements.txt
@@ -0,0 +1,2 @@
+flask==2.1.0
+flask-cors==3.0.8
diff --git a/3-docker-compose/app1/backend/server.py b/3-docker-compose/app1/backend/server.py
new file mode 100644
index 0000000..5765da9
--- /dev/null
+++ b/3-docker-compose/app1/backend/server.py
@@ -0,0 +1,15 @@
+from flask import Flask, render_template
+from flask_cors import CORS # need to mention
+import json
+
+app = Flask(__name__)
+CORS(app)
+
+@app.route('/book')
+def json_file():
+ file = open('./book.json')
+ json_data = json.load(file)
+ return json_data
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=5000)
diff --git a/3-docker-compose/app1/docker-compose.yml b/3-docker-compose/app1/docker-compose.yml
new file mode 100644
index 0000000..3aa2ca5
--- /dev/null
+++ b/3-docker-compose/app1/docker-compose.yml
@@ -0,0 +1,15 @@
+version: "3.9"
+
+services:
+ # 当前目录是docker-compose.yml所在文件夹
+ backend:
+ build: ./backend # 第一个项目Dockerfile所在文件夹
+ ports:
+ - "5000:5000" # 本地端口和容器内端口的映射
+ volumes:
+ - ./backend:/app # 将backend目录和容器内的/app目录绑定
+
+ frontend:
+ build: ./frontend # 第二个项目Dockerfile所在文件夹
+ ports:
+ - "3000:3000"
diff --git a/3-docker-compose/app1/frontend/Dockerfile b/3-docker-compose/app1/frontend/Dockerfile
new file mode 100644
index 0000000..eb4693c
--- /dev/null
+++ b/3-docker-compose/app1/frontend/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 3000
+ENTRYPOINT ["python3", "server.py"]
diff --git a/3-docker-compose/app1/frontend/requirements.txt b/3-docker-compose/app1/frontend/requirements.txt
new file mode 100644
index 0000000..aba55f1
--- /dev/null
+++ b/3-docker-compose/app1/frontend/requirements.txt
@@ -0,0 +1 @@
+flask==2.1.0
diff --git a/3-docker-compose/app1/frontend/server.py b/3-docker-compose/app1/frontend/server.py
new file mode 100644
index 0000000..52a1681
--- /dev/null
+++ b/3-docker-compose/app1/frontend/server.py
@@ -0,0 +1,10 @@
+from flask import Flask, render_template
+
+app = Flask(__name__)
+
+@app.route('/')
+def home():
+ return render_template('index.html')
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=3000)
diff --git a/3-docker-compose/app1/frontend/templates/index.html b/3-docker-compose/app1/frontend/templates/index.html
new file mode 100644
index 0000000..2314486
--- /dev/null
+++ b/3-docker-compose/app1/frontend/templates/index.html
@@ -0,0 +1,16 @@
+
+
+
+ Sample Book Info
+
+
+ Web App Sample Book
+
+
+
+
diff --git a/3-docker-compose/app2/backend/Dockerfile b/3-docker-compose/app2/backend/Dockerfile
new file mode 100644
index 0000000..09ee055
--- /dev/null
+++ b/3-docker-compose/app2/backend/Dockerfile
@@ -0,0 +1,6 @@
+FROM python:3.7
+COPY . /app
+WORKDIR /app
+RUN ["pip3", "install", "-r", "requirements.txt"]
+EXPOSE 5000
+CMD ["python3", "server.py"]
diff --git a/3-docker-compose/app2/backend/requirements.txt b/3-docker-compose/app2/backend/requirements.txt
new file mode 100644
index 0000000..4fb146d
--- /dev/null
+++ b/3-docker-compose/app2/backend/requirements.txt
@@ -0,0 +1,2 @@
+flask==2.1.0
+redis==3.5.3
diff --git a/3-docker-compose/app2/backend/server.py b/3-docker-compose/app2/backend/server.py
new file mode 100644
index 0000000..ccd03ce
--- /dev/null
+++ b/3-docker-compose/app2/backend/server.py
@@ -0,0 +1,16 @@
+from flask import Flask, render_template
+import redis
+import os
+REDIS_HOST = os.environ.get('REDIS_HOST', '0.0.0.0')
+
+app = Flask(__name__)
+cache = redis.Redis(host = REDIS_HOST, port = 6379, db = 0)
+cache.set('hits', 0)
+
+@app.route('/')
+def get_data():
+ hit_num = cache.incr('hits')
+ return f'I have been seen {hit_num} times.\n'
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', port=5000)
diff --git a/3-docker-compose/app2/docker-compose.yml b/3-docker-compose/app2/docker-compose.yml
new file mode 100644
index 0000000..82721a6
--- /dev/null
+++ b/3-docker-compose/app2/docker-compose.yml
@@ -0,0 +1,14 @@
+version: "3.9"
+
+services:
+
+ redis-server:
+ image: "redis"
+
+ backend:
+ environment:
+ - REDIS_HOST=redis-server
+ build: ./backend
+ ports:
+ - "5000:5000"
+
\ No newline at end of file
diff --git a/README.md b/README.md
index ecc1b4b..7048fd7 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-### docker学习仓库
+# docker-tutorial
\ No newline at end of file
diff --git a/docker_tutorial.code-workspace b/docker_tutorial.code-workspace
deleted file mode 100644
index 876a149..0000000
--- a/docker_tutorial.code-workspace
+++ /dev/null
@@ -1,8 +0,0 @@
-{
- "folders": [
- {
- "path": "."
- }
- ],
- "settings": {}
-}
\ No newline at end of file
diff --git a/python/Dockerfile b/python/Dockerfile
deleted file mode 100644
index df6ed86..0000000
--- a/python/Dockerfile
+++ /dev/null
@@ -1,8 +0,0 @@
-# 将image文件继承与官方的3.8版本的Python
-FROM python:3.8
-# 将当前目录下的所有文件(除了.dockerignore排除的路径),都拷贝进image文件的/app目录。
-COPY . /app
-# 指定接下来的工作路径为/app
-WORKDIR /app
-# 在/app目录下,运行python文件
-CMD python3 hello.py
\ No newline at end of file
diff --git a/python/hello.py b/python/hello.py
deleted file mode 100644
index e75154b..0000000
--- a/python/hello.py
+++ /dev/null
@@ -1 +0,0 @@
-print("hello world")
\ No newline at end of file