首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 11上的Docker运行映像显示错误/bin/sh: 1:[:python3:意外操作符

Windows 11上的Docker运行映像显示错误/bin/sh: 1:[:python3:意外操作符
EN

Stack Overflow用户
提问于 2022-05-04 08:15:31
回答 1查看 255关注 0票数 0

我已经在我的Windows 11笔记本上安装了以下内容:

在Docker桌面中,我启用了Kubernetes。

现在,我用Python创建了一个脚本,我称之为Books,它是一个API,它为json提供了书籍:

main.py:

代码语言:javascript
复制
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的文件:

文档:

代码语言:javascript
复制
# 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:

代码语言:javascript
复制
docker build --tag books .

当我现在打开Powershell来运行docker映像时,它会给出一个错误:

代码语言:javascript
复制
C:\Users\s>docker run books
/bin/sh: 1: [: python3: unexpected operator
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-04 08:33:13

您需要在Dockerfile中的命令之间设置一个逗号。ie:

代码语言:javascript
复制
CMD ["python3", "./main.py"]

见CMD格式这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72109792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档