Python API with Docker
A simple Flask API containerized using Docker, returning static JSON data about a friend (name, goals, and tech stack). This project serves as a practical demonstration of containerizing a basic Python application.
βοΈ Requirements
Docker installed on your machine (to build and run the container).
Python 3 (Optional, for local development/testing).
π Project Structure
This project contains the following files:
. βββ Dockerfile # Instructions for building the Docker image βββ app.py # The core Flask application that exposes the API endpoint βββ requirements.txt # Python dependencies (Flask) βββ README.md # This documentation file
π API Description
The app.py script runs a simple Flask web server on port 5000 inside the container. It exposes one root endpoint that returns the following JSON structure:
{ "name": "Alex", "goal_in_germany": "Master German and get a full-time software engineering role.", "tech_stack": ["Python", "JavaScript", "React", "Docker", "Kubernetes"] }
π³ Building and Running the Docker Image (Recommended)
This process allows you to run the application anywhere Docker is installed without needing a Python environment.
- Build the Image
Run the following command in the project's root directory. The -t flag tags the image with a custom name.
docker build -t my-python-api:1.0 .
- Run the Container
Run the image, mapping the container's internal port (5000) to port 5000 on your host machine.
docker run -d -p 5000:5000 --name friend-api-container my-python-api:1.0
The -d flag runs the container in detached (background) mode.
The --name assigns a descriptive name to the container.
The -p 5000:5000 maps HostPort:ContainerPort.
- Test the API
Access the endpoint using curl or open the URL in your browser:
- Cleanup
To stop and remove the running container:
docker stop friend-api-container docker rm friend-api-container
π Running Locally (Without Docker)
For developers who wish to debug or test the application directly:
Install Python dependencies:
pip install -r requirements.txt
Run the API:
python app.py
Test the API: Access the endpoint: http://localhost:5000/