What is API Communication?

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


The Problem — Missing Package

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.


Dockerfile — Correct Way

FROM python
WORKDIR /myapp
COPY ./app.py .
RUN pip install requests
CMD ["python", "app.py"]
docker build -t api-app .
docker run api-app

How It Works

Your Docker Container
        |
requests.get(url)      →   API Server: "Send me data"
        |
response.json()        ←   {"data": "value"}
        |
Print or use the data

Example 1 — Get Your IP Address

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