π Day 16 β Dockerfile + Docker Compose (Multi-Container Apps) | 30 Days DevOps Interview Prep

Series: 30 Days DevOps Interview Preparation
Author: Tathagat Gaikwad
In the previous sessions, we explored Docker basics (installation, images, running containers).
Today, we level up with Dockerfile and Docker Compose, which together allow us to build and run multi-container applicationsβa must-know for DevOps, Cloud, and SRE engineers.
π³ 1. Theory: Dockerfile
A Dockerfile is like a recipe for creating Docker images.
It contains instructions such as:
FROM β Base image
RUN β Commands to install dependencies
COPY/ADD β Add files into image
WORKDIR β Working directory
CMD/ENTRYPOINT β Default command to run
Example β Node.js App Dockerfile
# Use Node.js base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy app source code
COPY . .
# Expose app port
EXPOSE 3000
# Run the application
CMD ["node", "server.js"]
π 2. Theory: Docker Compose
While Dockerfile builds one container, most real-world apps need multiple services (e.g., backend + database).
Thatβs where Docker Compose comes in. It uses a docker-compose.yml
file to define and run multi-container applications.
Benefits:
β
Define multiple services in one file
β
Easy networking between services
β
Single command start/stop (docker-compose up -d
)
β
Supports volumes for persistent data
π 3. Practical on AWS (Step-by-Step)
Step 1: Launch EC2 Instance
Spin up an Ubuntu EC2 on AWS.
SSH into the instance.
sudo apt update -y
sudo apt install docker.io docker-compose -y
sudo usermod -aG docker ubuntu
newgrp docker
Step 2: Clone Application Repo
For demo, letβs create a simple Node.js app with MySQL.
mkdir loan-app && cd loan-app
nano server.js
server.js
const express = require("express");
const mysql = require("mysql2");
const app = express();
const db = mysql.createConnection({
host: "db",
user: "root",
password: "rootpass",
database: "loandb"
});
app.get("/", (req, res) => {
db.query("SELECT 'Hello from MySQL' AS message", (err, result) => {
if (err) throw err;
res.send(result[0].message);
});
});
app.listen(3000, () => console.log("Server running on port 3000"));
Step 3: Create Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Step 4: Create docker-compose.yml
version: "3.9"
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: loandb
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
Step 5: Run Containers
docker-compose up -d
Check running containers:
docker ps
Open browser β http://<EC2_Public_IP>:3000
β β
App connects to MySQL.
π― 4. Interview Preparation Questions
Q1. What is a Dockerfile?
π Itβs a set of instructions to build a Docker image automatically instead of doing it manually.
Q2. Difference between Dockerfile and Docker Compose?
π Dockerfile = build instructions for 1 image.
π Docker Compose = orchestrates multiple containers with networking, volumes, etc.
Q3. What is the benefit of Docker Compose?
π Simplifies multi-container app management, local dev/testing, version-controlled service definitions.
Q4. How do you persist database data in Docker Compose?
π By using volumes in the YAML file so data is not lost when containers restart.
Q5. Can Docker Compose be used in production?
π Yes, but not at large scale. Kubernetes is preferred in enterprise production.
Q6. How to optimize Dockerfiles?
π Use multi-stage builds, Alpine images, .dockerignore
, reduce layers, cache dependencies.
Q7. How does networking work in Docker Compose?
π By default, all services in a Compose file share a bridge network. Services can talk to each other using service names (e.g., db:3306
).
Q8. What are common mistakes with Docker Compose?
π Hardcoding passwords in YAML, not using volumes, forgetting health checks, and mismanaging network names.
β 5. Key Takeaways
Dockerfile builds portable images for apps.
Docker Compose orchestrates multi-container environments.
Together, they provide the foundation for microservices.
For interviews β Expect both theory + hands-on scenarios.
#DevOps #Docker #InterviewPreparation #Cloud #30DaysOfDevOps
Subscribe to my newsletter
Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
