Day 2 of 5
⏱ ~60 minutes
Docker and Cloud for AI — Day 2

Dockerfile: Containerizing Your AI Application

Day 2 writes the Dockerfile that packages your AI app. You will containerize a Python Flask API that calls an AI model.

Anatomy of a Dockerfile

Dockerfile for a Python AI App
# Start from official Python image
FROM python:3.11-slim

# Set working directory in container
WORKDIR /app

# Copy requirements first (Docker layer caching)
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose the port your app runs on
EXPOSE 8000

# Command to run when container starts
CMD ["python", "app.py"]
requirements.txt
flask==3.0.0
anthropic==0.34.0
python-dotenv==1.0.0
app.py
from flask import Flask, request, jsonify
import anthropic
import os

app = Flask(__name__)
client = anthropic.Anthropic(api_key=os.environ.get('ANTHROPIC_API_KEY'))

@app.route('/chat', methods=['POST'])
def chat():
    data = request.json
    message = data.get('message', '')
    
    response = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=1024,
        messages=[{'role': 'user', 'content': message}]
    )
    
    return jsonify({'reply': response.content[0].text})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=False)

Build and Run

Build and Test
# Build the image
docker build -t my-ai-app .

# Run it (passing API key as env var)
docker run -p 8000:8000   -e ANTHROPIC_API_KEY=your_key_here   my-ai-app

# Test it
curl -X POST http://localhost:8000/chat   -H "Content-Type: application/json"   -d '{"message": "Hello!"}'
💡
Layer caching: Copy requirements.txt before copying your code. Docker caches each layer — if requirements.txt hasn't changed, the pip install step is skipped. This makes rebuilds much faster during development.
Day 2 Exercise
Containerize Your AI App
  1. Create the three files: Dockerfile, requirements.txt, app.py.
  2. Build the image with docker build -t my-ai-app .
  3. Run the container with your API key as an environment variable.
  4. Test the /chat endpoint and verify you get responses.
  5. Change a line in app.py and rebuild. Notice the layer cache speeds it up.

Day 2 Summary

  • FROM, WORKDIR, COPY, RUN, EXPOSE, CMD — the six Dockerfile instructions you use most.
  • Copy requirements before code to maximize Docker layer caching.
  • API keys passed as -e environment variables at runtime, never baked into the image.
Finished this lesson?