Dockerizing a Conversational AI App with Persistent Storage
- Mar 24
- 6 min read

Course: Docker for AI Apps
Level: Beginner to Medium
Type: Individual
Duration: 5 to 7 days
Objective
This assignment tests your ability to work with Docker's core building blocks: running and inspecting containers, writing a production-ready Dockerfile, containerizing a Python AI application, and persisting data across container restarts using named volumes. By completing this assignment, you will have built and deployed a fully containerized multi-turn AI chatbot that retains conversation history even after the container is stopped and removed.
Problem Statement
You are required to build a multi-turn conversational AI chatbot using the OpenAI API, package it as a Docker image with a well-structured Dockerfile, and configure it to persist conversation history to a named Docker volume. The application must run correctly in an isolated container with no secrets baked into the image.
Tasks
Task 1: Docker Fundamentals Practice (15 marks)
Pull the official python:3.11-slim image and run a container from it interactively. Inside the container, verify the Python version and print the working directory.
Run an nginx container in detached mode on port 8080. Use docker ps to confirm it is running, then use docker inspect to find its IP address on the default bridge network.
Use docker logs to view the nginx access log after making a request to http://localhost:8080 in your browser.
Run docker system prune to clean up stopped containers, dangling images, and unused networks. Record the space reclaimed. Include screenshots or terminal output for each step.
Task 2: Write a Production-Ready Dockerfile (20 marks)
Write a Dockerfile for a Python 3.11 application that follows best-practice layer ordering: system dependencies first, then Python dependencies (requirements.txt), then application code last.
Use python:3.11-slim as the base image. Add a .dockerignore file that excludes .env, pycache, .git, and *.pyc files.
Set WORKDIR to /app, set PYTHONUNBUFFERED=1 as an environment variable, and use EXPOSE to document the application port.
Build the image and run docker history on it to show the layer breakdown. Write a short explanation (100 to 150 words) of why the COPY order matters for layer caching.
Compare the final image size against python:3.11 (full) and python:3.11-alpine. Record all three sizes and explain the trade-offs of each base image choice.
Task 3: Containerize a Multi-Turn AI Chatbot (30 marks)
Write a chatbot.py script that maintains a conversation history list and sends the full history to gpt-4o-mini with each user message, allowing genuine multi-turn conversation.
The application must read the OpenAI API key from the OPENAI_API_KEY environment variable. The key must never appear in the Dockerfile, the image, or any committed file.
Create a .env file containing your API key and pass it to the container at runtime using the --env-file flag. Confirm the container starts and responds correctly.
Set PYTHONUNBUFFERED=1 so that print output appears in real time. Run the container in interactive mode using docker run -it --env-file .env. Demonstrate at least 3 turns of conversation in your submitted terminal output.
Show the command used to build the image, the command used to run it, and the full conversation output in your submission.
Task 4: Add Persistent Conversation History with Named Volumes (25 marks)
Upgrade chatbot.py to save the full conversation history to /data/history.json after every message. On startup, load the existing history from that file if it exists, so the conversation continues across container restarts.
Create a named Docker volume called chat-history. Mount it to /data inside the container using the -v flag.
Run the chatbot, have a 3-turn conversation, then stop and remove the container. Start a new container from the same image with the same volume mounted. Confirm the conversation history is preserved and the new session continues from where the previous one left off.
Use docker volume inspect chat-history to show the mount point on the host. Include the output in your submission.
Explain in 80 to 120 words why named volumes persist data even after docker rm, and why this is preferable to writing history to the container filesystem.
Task 5: Analysis and Reflection (10 marks)
Write a reflection (200 to 300 words) covering: what the 'works on my machine' problem is and how Docker solves it for AI applications specifically; one mistake you made during this assignment and how you diagnosed and fixed it; and one thing you would do differently if packaging this application for a production environment.
Evaluation Rubric
Criteria | Marks |
Docker Fundamentals Practice | 15 |
Production-Ready Dockerfile | 20 |
Containerized Multi-Turn Chatbot | 30 |
Persistent Storage with Named Volumes | 25 |
Analysis and Reflection | 10 |
Total | 100 |
Deliverables
A project directory containing: chatbot.py, Dockerfile, requirements.txt, .dockerignore, and a .env.example file (with a placeholder key, not your real key).
Terminal output screenshots or a transcript showing: docker build, docker run, the multi-turn conversation, the container restart, and the volume inspect output.
A written reflection (200 to 300 words) either in a separate reflection.md file or at the bottom of a README.md.
A README.md with instructions for building the image, creating the volume, and running the container.
Submission Guidelines
Submit your work via the course LMS (for example, Moodle or Google Classroom).
File Naming Convention: <YourName>_Docker_Assignment1.zip
Inside the ZIP:
Dockerfile
requirements.txt
.dockerignore
.env.example
reflection.md (or included in README.md)
screenshots/ (folder with terminal output images)
Deadline: 7 days from the date of release.
Late Submission Policy
Up to 24 hours late: 10% penalty applied to the final mark.
24 to 48 hours late: 20% penalty applied to the final mark.
Beyond 48 hours: submission will not be accepted.
Important Instructions
Never include your real OpenAI API key in any submitted file. Submit a .env.example with a placeholder value such as OPENAI_API_KEY=your-key-here.
Do not use CMD ["python", "-u"] as a workaround for buffering issues. Set PYTHONUNBUFFERED=1 as an ENV instruction in the Dockerfile.
The image must build successfully from scratch using only docker build and docker run. Test this by building from a clean state before submitting.
Do not hardcode the model name in the Dockerfile. Pass it as an environment variable or keep it in the Python source file.
Plagiarism of any kind will result in disqualification from the assignment.
Guidance and Tips
Write your requirements.txt before writing the Dockerfile. Install dependencies from the file in the image, not by running pip install manually inside a running container.
Test your .dockerignore by checking that docker build does not include your .env file in the build context. Use docker run --rm <image> ls -la to verify.
If the chatbot does not show output immediately, check that PYTHONUNBUFFERED=1 is set and that you are flushing stdout (use print(..., flush=True) as an alternative).
Named volumes are managed by Docker, not by you. Do not try to find the volume files manually — use docker volume inspect to find the mount path.
Think about what happens if history.json does not exist yet on first run. Handle the FileNotFoundError gracefully in your Python code.
Bonus (Optional — up to +10 Marks)
Add a --reset flag to chatbot.py that clears the history file and starts a fresh conversation when passed as a command-line argument.
Use a bind mount instead of a named volume and demonstrate the difference in how Docker manages the storage location on the host.
Add a system prompt as a configurable ENV variable in the Dockerfile so the chatbot persona can be changed at runtime without rebuilding the image.
Instructor Note
This assignment is designed to give you hands-on experience with the most fundamental Docker skills used in real AI projects. There is no single correct Dockerfile. What matters is that your image builds cleanly, your secrets are handled correctly, your application runs as expected inside the container, and you can explain every instruction you wrote. A well-documented submission that demonstrates genuine understanding will always score better than a working submission with no explanation.
Call to Action
Ready to transform your business with AI-powered intelligence that accelerates insights, enhances decision-making, and unlocks the full value of your data?
Codersarts is here to help you turn complex data workflows into efficient, scalable, and evidence-driven AI systems that empower teams to make smarter, faster, and more confident decisions.
Whether you’re a startup looking to build AI-driven products, an enterprise aiming to optimize operations through data science, or a research organization advancing innovation with intelligent data solutions, we bring the expertise and experience needed to design, develop, and deploy impactful AI systems that drive measurable business outcomes.
Get Started Today
Schedule an AI & Data Science Consultation:
Book a 30-minute discovery call with our AI strategists and data science experts to discuss your challenges, identify high-impact opportunities, and explore how intelligent AI solutions can transform your workflows and performance.
Request a Custom AI Demo:
Experience AI in action with a personalized demonstration built around your business use cases, datasets, operational environment, and decision workflows — showcasing practical value and real-world impact.
Email: contact@codersarts.com
Transform your organization from data accumulation to intelligent decision enablement — accelerating insight generation, improving operational efficiency, and strengthening competitive advantage.
Partner with Codersarts to build scalable AI solutions including RAG systems, predictive analytics platforms, intelligent automation tools, recommendation engines, and custom machine learning models that empower your teams to deliver exceptional results.
Contact us today and take the first step toward next-generation AI and data science capabilities that grow with your business ambitions.




Comments