exercises/docker/docker-debugging.md
This file contains scenario-based Docker questions to help DevOps engineers practice real-world troubleshooting and configuration tasks. Each question simulates a practical scenario with a step-by-step answer.
You’re a DevOps engineer deploying a Node.js application using Docker. You run docker run -d -p 3000:3000 my-node-app, but the container exits immediately. Using docker ps -a, you see the container status as Exited. How would you troubleshoot and resolve this issue?
To troubleshoot a container exiting immediately:
docker logs my-node-app to view error messages. Common issues include missing dependencies (e.g., npm install failed) or an incorrect command.docker inspect my-node-app to check Config.Cmd or Config.Entrypoint. Ensure the command (e.g., node app.js) is valid.CMD or ENTRYPOINT is correct, e.g., CMD ["node", "app.js"]. Update and rebuild if needed: docker build -t my-node-app ..docker run -it my-node-app sh to debug manually (e.g., node app.js).docker stats.Example Fix: If logs show node: command not found, update the Dockerfile to use FROM node:18, rebuild, and rerun.
docker logs for error clues.docker ps -a to check container status and ID.As a DevOps engineer, you need to deploy a web application with a Node.js backend and a MySQL database using Docker. The Node.js app connects to MySQL on localhost:3306, but running docker run for each container separately fails because they can’t communicate. How would you set up these containers to work together?
To make the Node.js and MySQL containers communicate:
docker-compose.yml file to define and link both services:
version: '3.8'
services:
node-app:
image: my-node-app
build: .
ports:
- "3000:3000"
depends_on:
- mysql-db
environment:
- DB_HOST=mysql-db
- DB_PORT=3306
mysql-db:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=secret
ports:
- "3306:3306"
docker-compose up -d to start both containers. The node-app service connects to mysql-db using the service name (mysql-db) as the hostname, not localhost.docker-compose logs node-app to ensure the Node.js app connects to MySQL. If it fails, verify the environment variables and MySQL’s readiness.docker network create my-app-networkdocker run -d --name mysql-db --network my-app-network -e MYSQL_ROOT_PASSWORD=secret mysql:8.0docker run -d --name node-app --network my-app-network -p 3000:3000 -e DB_HOST=mysql-db my-node-appYou’re a DevOps engineer integrating a Dockerized Python application into a Jenkins CI/CD pipeline. The Dockerfile builds slowly, causing pipeline delays. How would you optimize the Dockerfile to speed up builds while maintaining functionality?
To optimize a Dockerfile for faster CI/CD builds:
python:3.9 with python:3.9-slim to reduce size and download time.
FROM python:3.9-slim
requirements.txt and install dependencies before copying the app code:
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
&& to reduce layers:
RUN pip install -r requirements.txt && rm -rf /root/.cache/pip
FROM python:3.9 AS builder
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM python:3.9-slim
COPY --from=builder /usr/local/lib/python3.9 /usr/local/lib/python3.9
COPY . .
CMD ["python", "app.py"]
Dockerfile or code changes, using a cached image otherwise:
pipeline {
agent any
stages {
stage('Build Docker Image') {
when { changeset "Dockerfile,**.py" }
steps { sh 'docker build -t my-python-app .' }
}
}
}
.dockerignore to exclude unnecessary files (e.g., .git, tests/) from the build context.Contributed by Lahiru Galhena