πŸš€ 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

0
Subscribe to my newsletter

Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tathagat Gaikwad
Tathagat Gaikwad