No module error requests in docker container

10/17/2019

Getting error on Kubernetes container, No module named 'requests' even though I installed it using pip and also test multiple Docker images.

Docker file:-

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" ]

app-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__":
-- gaurav agnihotri
docker
kubernetes
python

1 Answer

10/17/2019

Try this, I hope this may help you.

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" ]
-- Neda Peyrone
Source: StackOverflow