# Use the official Python image from the Docker Hub
FROM python:3.9-alpine
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN apk add --no-cache curl# 与sh(Bourne Shell)相比,Bash包含了更多的功能和特性
RUN apk add --no-cache bash# Make port 5000 available to the world outside this container
EXPOSE 5000# Run app.py when the container launches
CMD ["python", "app.py"]
from flask import Flask, request, jsonify
from dotenv import load_dotenv
import os
app = Flask(__name__)
@app.route('/')
def hello():
return"Hello, World!"# curl -X POST http://127.0.0.1:5000/data -H "Content-Type: application/json" -d '{"name": "Alice", "age": 30}'# response :{"age_in_5_years":35,"message":"Hello, Alice!"}
@app.route('/data', methods=['POST'])
def process_data():
# 获取请求中的 JSON 数据
data = request.get_json()# 提取需要的入参,例如 "name" 和 "age"
name = data.get('name', 'Unknown')
age = data.get('age', 0)# 打印入参(可选)
print(f"Received name: {name}, age: {age}")# 返回一个响应
response ={'message': f'Hello, {name}!',
'age_in_5_years': age + 5}return jsonify(response)
def print_hi(name):
print(f'Hi, {name}')if __name__ =='__main__':
print_hi('PyCharm')
load_dotenv('config/dev.env')
example_var = os.getenv('EXAMPLE_VAR')
print('The value of EXAMPLE_VAR is:', example_var)
var1 = os.getenv('var1')
print('The value of var1 is:', var1)
app.run(host='0.0.0.0', port=5000)