API (Application Programming Interface) is a way for your code to talk to external services and get data.
Example: Your code asks a weather website "What is the temperature?" and gets back "25C".
Python needs the requests library to make API calls. Docker does not have it by default so you get an error.
ModuleNotFoundError: No module named 'requests'
Fix: install it in your Dockerfile using RUN pip install requests.
FROM python
WORKDIR /myapp
COPY ./app.py .
RUN pip install requests
CMD ["python", "app.py"]
docker build -t api-app .
docker run api-app
Your Docker Container
|
requests.get(url) → API Server: "Send me data"
|
response.json() ← {"data": "value"}
|
Print or use the data
import requests
response = requests.get("<https://api.ipify.org?format=json>")
data = response.json()
print(f"Your IP: {data['ip']}")
FROM python
WORKDIR /myapp
COPY ./ip_check.py .
RUN pip install requests
CMD ["python", "ip_check.py"]