This project demonstrates how to efficiently use Docker to boost productivity when developing, testing, and deploying a Flask application with Redis. It showcases various Docker tools like:
- Docker Desktop (GUI for managing containers)
- Docker Compose (Multi-container orchestration)
- Docker Compose Watch (Live code reloading)
- Docker Scout (Security scanning)
- Docker Build Cloud (Faster builds)
- Testcontainers (Automated testing in isolated environments)
- Install Docker Desktop
- Install Python 3.9+
- Install Redis (Optional, since it will run inside a container)
- Install pytest (for running automated tests)
.
βββ app.py # Flask application
|__ templates # html file
βββ Dockerfile # Docker instructions to build image
βββ docker-compose.yml # Multi-container setup
βββ .dockerignore # Ignore unnecessary files
βββ requirements.txt # Python dependencies
βββ test_app.py # Testcases using Testcontainers
βββ README.md # Documentation
git clone https://github.com/N4si/docker-project.git
cd docker-project
pip3 install -r requirements.txt
python3 app.py # Run Flask app on localhost:5001Check the application at http://localhost:5001.
Test Redis connection:
redis-cli
KEYS *
LRANGE metrics 0 -1# Use the official Python image
FROM python:3.9
# Set working directory
WORKDIR /app
# Copy files
COPY . .
# Install dependencies
RUN pip install --no-cache-dir flask psutil redis
# Expose the port
EXPOSE 5001
# Run the application
CMD ["flask", "run", "--host=0.0.0.0", "--port=5001", "--debug"]docker build -t flask-monitor .
docker run -p 5001:5001 flask-monitorservices:
web:
build: .
ports:
- "5001:5001"
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- FLASK_APP=app.py
- FLASK_ENV=development
- FLASK_RUN_HOST=0.0.0.0
- FLASK_RUN_PORT=5001
depends_on:
- redis
develop:
watch:
- action: sync
path: .
target: /app
command: flask run
redis:
image: "redis:latest"
ports:
- "6379:6379"docker compose up -ddocker compose downdocker compose watchThis will sync code changes automatically without rebuilding the container!
docker scout quickview flask-monitor
docker scout cves flask-monitorUse docker scout to get security insights and remediation steps!
docker buildx version
docker buildx create --name mybuilder --use
docker buildx bake --progress=plainFaster builds by caching and running parallel builds.
pip3 install pytest testcontainersimport pytest
import redis
import os
from testcontainers.redis import RedisContainer
from app import app
@pytest.fixture(scope="module")
def redis_container():
with RedisContainer() as redis_container:
redis_host = redis_container.get_container_host_ip()
redis_port = redis_container.get_exposed_port(6379)
os.environ["REDIS_HOST"] = redis_host
os.environ["REDIS_PORT"] = str(redis_port)
yield redis_host, redis_port
@pytest.fixture
def client():
app.config["TESTING"] = True
with app.test_client() as client:
yield client
def test_redis_connection(redis_container):
redis_host, redis_port = redis_container
r = redis.Redis(host=redis_host, port=int(redis_port), decode_responses=True)
r.set("test_key", "Hello, Redis!")
assert r.get("test_key") == "Hello, Redis!"
def test_metrics_endpoint(client):
response = client.get("/metrics")
assert response.status_code == 200
data = response.get_json()
assert "cpu" in data
assert "memory" in data
assert "disk" in datapython3 -m pytest test_app.py| Feature | Command |
|---|---|
| Build Image | docker build -t flask-monitor . |
| Run Container | docker run -p 5001:5001 flask-monitor |
| Start Services | docker compose up -d |
| Stop Services | docker compose down |
| Live Reload | docker compose watch |
| Security Scan | docker scout quickview flask-monitor |
| Faster Build | docker buildx bake --progress=plain |
| Run Tests | pytest test_app.py |
By using these Docker tools, you can:
- Develop faster with Docker Compose Watch π
- Ensure security with Docker Scout π
- Optimize builds using Docker Build Cloud π
- Automate testing with Testcontainers β
π Now, you're ready to boost your productivity with Docker!
π‘ Feel free to contribute & star β the repo!