I have installed the following on my Windows 11 laptop:
- Python 3.10
- Pycharm editor
- WSL 2 from https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
- Latest Ubuntu from https://apps.microsoft.com/store/detail/ubuntu/9PDXGNCFSCZV?hl=nb-no&gl=no&rtc=1
- Docker desktop from https://desktop.docker.com/win/main/amd64/Docker Desktop Installer.exe
In Docker desktop I have enabled Kubernetes.
Now I have created a script in Python that I have called Books that is a API that gives a json with books:
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()
When I run the script from Pycharm everything works fine. I can view the API at http://127.0.0.1:5000/api/v1/resources/books/all
Now I try to build a docker image. I have created a file named dockerfile:
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" ]
I build the docker image with the following command, and it build OK:
docker build --tag books .
When I now open Powershell to run the docker image it gives me an error:
C:\Users\s>docker run books
/bin/sh: 1: [: python3: unexpected operator
CodePudding user response:
You need a comma between commands in your Dockerfile. ie:
CMD ["python3", "./main.py"]