我已经在我的Windows 11笔记本上安装了以下内容:
在Docker桌面中,我启用了Kubernetes。
现在,我用Python创建了一个脚本,我称之为Books,它是一个API,它为json提供了书籍:
main.py:
import flask
from flask import jsonify, request
app = flask.Flask(__name__) # Creates the Flask application object
app.config["DEBUG"] = True
# Create some test data for our catalog in the form of a list of dictionaries.
books = [
{'id': 0,
'title': 'A Fire Upon the Deep',
'author': 'Vernor Vinge',
'first_sentence': 'The coldsleep itself was dreamless.',
'year_published': '1992'},
{'id': 1,
'title': 'The Ones Who Walk Away From Omelas',
'author': 'Ursula K. Le Guin',
'first_sentence': 'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.',
'published': '1973'},
{'id': 2,
'title': 'Dhalgren',
'author': 'Samuel R. Delany',
'first_sentence': 'to wound the autumnal city.',
'published': '1975'}
]
@app.route('/', methods=['GET'])
def home():
return '''<h1>Distant Reading Archive</h1>
<p>A prototype API for distant reading of science fiction novels.</p>'''
@app.route('/api/v1/resources/books/all', methods=['GET'])
def api_all():
return jsonify(books)
@app.route('/api/v1/resources/books', methods=['GET'])
def api_id():
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
if 'id' in request.args:
id = int(request.args['id'])
else:
return "Error: No id field provided. Please specify an id."
# Create an empty list for our results
results = []
# Loop through the data and match results that fit the requested ID.
# IDs are unique, but other fields might return many results
for book in books:
if book['id'] == id:
results.append(book)
# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)
app.run()当我从Pycharm运行这个脚本时,一切都很好。我可以在http://127.0.0.1:5000/api/v1/resources/books/all上查看API
现在我试着建立一个码头形象。我创建了一个名为dockerfile的文件:
文档:
# Specifying Python
FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install python3 -y
RUN apt-get install python3-pip -y
RUN pip install --upgrade pip
# Add Python script
ADD main.py main.py
# Install dependencies
# RUN pip install -r requirements.txt
RUN pip install flask
# Run script
CMD [ "python3" "./main.py" ]我使用以下命令构建了docker映像,并且它构建了OK:
docker build --tag books .当我现在打开Powershell来运行docker映像时,它会给出一个错误:
C:\Users\s>docker run books
/bin/sh: 1: [: python3: unexpected operator发布于 2022-05-04 08:33:13
https://stackoverflow.com/questions/72109792
复制相似问题