📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Microservices Containerising Microservices

Containerising Microservices

5 min read
Containerise each service with non-root user, healthcheck, and Compose for local multi-service development.

Containerising Microservices

FROM python:3.12-slim

RUN useradd -r appuser
WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir
COPY . .
USER appuser

EXPOSE 8000
HEALTHCHECK --interval=30s 
  CMD curl -f http://localhost:8000/health || exit 1
CMD ["uvicorn","main:app","--host","0.0.0.0","--port","8000"]

# docker-compose for local dev
services:
  user-service:
    build: ./user-service
    ports: ["3001:8000"]
    environment:
      DATABASE_URL: postgresql://user:pass@db:5432/users
    depends_on:
      db: {condition: service_healthy}