docker_tutorial

master
barney 2 years ago
parent ee8bdc2fef
commit aa2059a38c
  1. 2
      1-intro/.dockerignore
  2. 8
      1-intro/Dockerfile
  3. 1
      1-intro/hello.py
  4. 4
      2-common-commands/pyramid/Dockerfile
  5. 11
      2-common-commands/pyramid/pyramid.py
  6. 6
      2-common-commands/volume/Dockerfile
  7. 2
      2-common-commands/volume/requirements.txt
  8. 17
      2-common-commands/volume/server.py
  9. 17
      2-common-commands/volume/templates/index.html
  10. 3
      2-common-commands/volume/volume_data/book.json
  11. 6
      2-common-commands/webapp/backend/Dockerfile
  12. 5
      2-common-commands/webapp/backend/book.json
  13. 2
      2-common-commands/webapp/backend/requirements.txt
  14. 15
      2-common-commands/webapp/backend/server.py
  15. 6
      2-common-commands/webapp/frontend/Dockerfile
  16. 1
      2-common-commands/webapp/frontend/requirements.txt
  17. 10
      2-common-commands/webapp/frontend/server.py
  18. 16
      2-common-commands/webapp/frontend/templates/index.html
  19. 6
      3-docker-compose/app1/backend/Dockerfile
  20. 5
      3-docker-compose/app1/backend/book.json
  21. 2
      3-docker-compose/app1/backend/requirements.txt
  22. 15
      3-docker-compose/app1/backend/server.py
  23. 15
      3-docker-compose/app1/docker-compose.yml
  24. 6
      3-docker-compose/app1/frontend/Dockerfile
  25. 1
      3-docker-compose/app1/frontend/requirements.txt
  26. 10
      3-docker-compose/app1/frontend/server.py
  27. 16
      3-docker-compose/app1/frontend/templates/index.html
  28. 6
      3-docker-compose/app2/backend/Dockerfile
  29. 2
      3-docker-compose/app2/backend/requirements.txt
  30. 16
      3-docker-compose/app2/backend/server.py
  31. 14
      3-docker-compose/app2/docker-compose.yml
  32. 2
      README.md
  33. 8
      docker_tutorial.code-workspace
  34. 8
      python/Dockerfile
  35. 1
      python/hello.py

@ -1,2 +1,2 @@
__pycache__ __pycache__
env env

@ -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

@ -0,0 +1 @@
print('Hello World')

@ -0,0 +1,4 @@
FROM python:3.7
COPY ./pyramid.py /pyramid.py
ENTRYPOINT ["python3", "pyramid.py"]
CMD ["5"] # 指定参数时会被覆盖

@ -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)

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 3000
CMD ["python3", "server.py"]

@ -0,0 +1,2 @@
flask==2.1.0
flask-cors==3.0.8

@ -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)

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Sample Book Info</title>
</head>
<body>
<h1>Volume Sample Book</h1>
<div id='content-info'></div>
<script>
fetch('./book')
.then(response => response.json())
.then(data => {
document.querySelector('#content-info').innerText = data.title
})
</script>
</body>
</html>

@ -0,0 +1,3 @@
{
"title": "The Book Title"
}

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 5000
CMD ["python3", "server.py"]

@ -0,0 +1,5 @@
{
"title": "Book Title",
"description": "Description",
"link": "turingplanet.org"
}

@ -0,0 +1,2 @@
flask==2.1.0
flask-cors==3.0.8

@ -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)

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 3000
ENTRYPOINT ["python3", "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)

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Sample Book Info</title>
</head>
<body>
<h1>Web App Sample Book</h1>
<div id='content-info'></div>
<script>
fetch('http://192.168.153.131:5000/book')
.then(response => response.json())
.then(data => { document.querySelector('#content-info').innerText = data.title
})
</script>
</body>
</html>

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 5000
CMD ["python3", "server.py"]

@ -0,0 +1,5 @@
{
"title": "Title_1",
"description": "Description",
"link": "turingplanet.org"
}

@ -0,0 +1,2 @@
flask==2.1.0
flask-cors==3.0.8

@ -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)

@ -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"

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 3000
ENTRYPOINT ["python3", "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)

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Sample Book Info</title>
</head>
<body>
<h1>Web App Sample Book</h1>
<div id='content-info'></div>
<script>
fetch('http://192.168.153.131:5000/book')
.then(response => response.json())
.then(data => { document.querySelector('#content-info').innerText = data.title
})
</script>
</body>
</html>

@ -0,0 +1,6 @@
FROM python:3.7
COPY . /app
WORKDIR /app
RUN ["pip3", "install", "-r", "requirements.txt"]
EXPOSE 5000
CMD ["python3", "server.py"]

@ -0,0 +1,2 @@
flask==2.1.0
redis==3.5.3

@ -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)

@ -0,0 +1,14 @@
version: "3.9"
services:
redis-server:
image: "redis"
backend:
environment:
- REDIS_HOST=redis-server
build: ./backend
ports:
- "5000:5000"

@ -1 +1 @@
### docker学习仓库 # docker-tutorial

@ -1,8 +0,0 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

@ -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

@ -1 +0,0 @@
print("hello world")
Loading…
Cancel
Save