mac-arm架构本地build案例

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

# 拉取指定架构的镜像
docker pull --platform linux/amd64 python:3.9-alpine 
# 跨架构编译
docker buildx build --platform linux/amd64 -t test-conda:1.0 -f Dockerfile .

docker save -o test-conda:1.0-x86.tar.gz test-conda:1.0

# 运行
docker rm -f test-conda && docker run -itd --name test-conda -e var1="xxx" test-conda:1.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值