To create a sample REST API using the aiohttp library in Python and deploy it using Docker, follow these steps:
- Install Dependencies
Before starting, ensure that you have aiohttp and docker installed. You can install aiohttp using pip:
pip install aiohttp
- Create a Basic aiohttp API
Create a file named app.py to implement a simple REST API using aiohttp. app.py
import aiohttp
from aiohttp import web
# A simple handler for the / endpoint
async def handle(request):
return web.json_response({'message': 'Hello, World!'})
# Another endpoint with a dynamic route
async def greet(request):
name = request.match_info.get('name', "Anonymous")
return web.json_response({'message': f'Hello, {name}!'})
# Application setup
app = web.Application()
# Define routes
app.router.add_get('/', handle)
app.router.add_get('/greet/{name}', greet)
# Running the application
if __name__ == '__main__':
web.run_app(app, host='0.0.0.0', port=8080)
- Create a Dockerfile
Next, you need to create a Dockerfile to containerize your aiohttp application. Dockerfile
# Use Python 3.11 image as base
FROM python:3.11-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the application code into the container
COPY app.py /app/
# Install the required Python dependencies
RUN pip install --no-cache-dir aiohttp
# Expose the port the app runs on
EXPOSE 8080
# Command to run the application
CMD ["python", "app.py"]
- Build and Run Docker Image
Now that you have both your app.py and Dockerfile, you can build the Docker image.
Build the Docker image:
docker build -t aiohttp-api .
Run the Docker container:
docker run -d -p 8080:8080 aiohttp-api
This will run the aiohttp API in a container, and you'll be able to access it on http://localhost:8080. 5. Test the API
Once the container is running, you can test the API using curl, Postman, or a web browser.
Test the root endpoint:
You should receive:
{"message": "Hello, World!"}
Test the greet endpoint:
curl http://localhost:8080/greet/John
You should receive:
{"message": "Hello, John!"}
- Stopping the Container
When you're done, you can stop the container by finding its container ID with docker ps, and then using the docker stop command.
docker ps docker stop <container_id>
Conclusion
You have successfully created a simple REST API using the aiohttp library in Python and containerized it using Docker. The application serves two endpoints:
/ returns a "Hello, World!" message.
/greet/{name} returns a personalized greeting.
This is a basic setup to get you started with asynchronous APIs in Python and Docker. You can extend this by adding more functionality, such as database access, authentication, and more sophisticated routing.