![](/img/trans.png)
[英]ModuleNotFoundError: No module named 'requests' error for docker run
[英]No module error requests in docker container
在 Kubernetes 容器上出現錯誤,即使我使用 pip 安裝它並測試多個 Docker 圖像,也沒有名為“請求”的模塊。
Docker 文件:-
FROM jfloff/alpine-python:2.7
MAINTAINER "Gaurav Agnihotri"
#choosing /usr/src/app as working directory
WORKDIR /usr/src/app
# Mentioned python module name to run application
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install requests==2.7.0
# Exposing applicaiton on 80 so that it can be accessible on 80
EXPOSE 80
#Copying code to working directory
COPY . .
#Making default entry as python will launch api.py
CMD [ "python", "app-1.py" ]
應用程序-1.py
#!/usr/bin/env python
import random
import requests
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api', methods=['POST'])
def api():
user_data = request.get_json()
data = user_data['message']
r = requests.post('http://localhost:5000/reverse', json={'message': data })
json_resp = r.json()
a = random.uniform(0, 10)
return jsonify({"rand": a, "message": json_resp.get("message")})
if __name__ == "__main__":
試試這個,我希望這可以幫助你。
Dockerfile:
FROM ubuntu:18.10
RUN apt-get update -y && \
apt-get install -y python-pip python-dev
# Set the working directory to /usr/src/app
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
ADD . /usr/src/app
RUN pip install -r requirements.txt
ENTRYPOINT [ "python" ]
CMD [ "app-1.py" ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.