Quiz: Docker and CI/CD
Test your knowledge on Docker, Docker Compose, and GitHub Actions.
30 questions - Click on each question to reveal the answer.
Questions 1-15: Docker
Question 1: What is Docker?
Answer: Docker is a containerization platform that allows packaging an application with all its dependencies into a lightweight and portable container. Advantages:
- Same environment everywhere
- Application isolation
- Fast deployment
- Scalability
Question 2: What is the difference between an image and a container?
Answer:
- Image: read-only template (like a class)
- Container: running instance of an image (like an object)
# Create a container from an image
docker run -d --name my-app my-image:1.0
Question 3: How to write a Dockerfile for Spring Boot?
Answer:
FROM eclipse-temurin:21-jdk-alpine
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Question 4: How to optimize a Dockerfile with multi-stage build?
Answer:
# Stage 1: Build
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -DskipTests
# Stage 2: Run
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Final image is smaller (no Maven, full JDK).
Question 5: How to build and run a Docker image?
Answer:
# Build the image
docker build -t my-app:1.0 .
# Run
docker run -d -p 8080:8080 --name my-app my-app:1.0
# View logs
docker logs -f my-app
# Stop
docker stop my-app
# Remove
docker rm my-app
Question 6: How to pass environment variables to the container?
Answer:
# In command line
docker run -e SPRING_PROFILES_ACTIVE=prod \
-e DB_HOST=postgres \
my-app:1.0
# With a file
docker run --env-file .env my-app:1.0
# .env
SPRING_PROFILES_ACTIVE=prod
DB_HOST=postgres
DB_PASSWORD=secret
Question 7: How to persist data with Docker?
Answer: With volumes:
# Named volume
docker run -v postgres-data:/var/lib/postgresql/data postgres
# Bind mount
docker run -v $(pwd)/data:/app/data my-app
Data survives container deletion.
Question 8: How to connect containers to each other?
Answer: With a Docker network:
# Create a network
docker network create my-network
# Connect containers
docker run -d --name postgres --network my-network postgres
docker run -d --name app --network my-network \
-e DB_HOST=postgres my-app
# Containers find each other by name
Question 9: What is Docker Compose?
Answer: Tool to define and run multi-container applications:
# docker-compose.yml
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- postgres
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/mydb
postgres:
image: postgres:15
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=mydb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
postgres-data:
Question 10: What are the main Docker Compose commands?
Answer:
# Start all services
docker compose up -d
# View logs
docker compose logs -f
# Stop
docker compose stop
# Remove (with volumes)
docker compose down -v
# Rebuild
docker compose up -d --build
Question 11: How to configure a Docker healthcheck?
Answer:
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD curl -f http://localhost:8080/actuator/health || exit 1
Or in docker-compose.yml:
services:
app:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 60s
Question 12: How to limit a container's resources?
Answer:
services:
app:
deploy:
resources:
limits:
cpus: '1'
memory: 512M
reservations:
cpus: '0.5'
memory: 256M
Question 13: How to view containers and images?
Answer:
# Running containers
docker ps
# All containers
docker ps -a
# Images
docker images
# Disk usage
docker system df
# Clean up
docker system prune -a
Question 14: How to debug a container?
Answer:
# Logs
docker logs my-app
# Shell into the container
docker exec -it my-app /bin/sh
# Inspect the container
docker inspect my-app
# Real-time stats
docker stats my-app
Question 15: How to push an image to a registry?
Answer:
# Docker Hub
docker login
docker tag my-app:1.0 username/my-app:1.0
docker push username/my-app:1.0
# GitHub Container Registry
docker login ghcr.io
docker tag my-app:1.0 ghcr.io/username/my-app:1.0
docker push ghcr.io/username/my-app:1.0
Questions 16-30: CI/CD with GitHub Actions
Question 16: What is GitHub Actions?
Answer: CI/CD automation platform integrated with GitHub:
- Workflows triggered by events
- Hosted or self-hosted runners
- Marketplace of reusable actions
- Secure secrets and variables
Question 17: How to structure a GitHub Actions workflow?
Answer:
# .github/workflows/ci.yml
name: CI Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Build with Maven
run: mvn clean package
Question 18: How to run tests in CI?
Answer:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: 'maven'
- name: Run tests
run: mvn test
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: target/surefire-reports/
Question 19: How to use Maven cache?
Answer:
- uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: 'maven' # Automatic cache
# Or manually
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
Question 20: How to build and push a Docker image?
Answer:
jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
Question 21: How to use GitHub secrets?
Answer:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy to server
env:
SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: |
echo "$SSH_KEY" > key.pem
chmod 600 key.pem
ssh -i key.pem user@server "deploy.sh"
Secrets are masked in logs.
Question 22: How to run jobs in parallel?
Answer:
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: mvn test
lint:
runs-on: ubuntu-latest
steps:
- run: mvn checkstyle:check
security:
runs-on: ubuntu-latest
steps:
- run: mvn dependency-check:check
By default, jobs run in parallel.
Question 23: How to create dependencies between jobs?
Answer:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: mvn package
test:
needs: build # Waits for build to finish
runs-on: ubuntu-latest
steps:
- run: mvn test
deploy:
needs: [build, test] # Waits for both
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh
Question 24: How to use a matrix to test multiple versions?
Answer:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
java: [17, 21]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: 'temurin'
- run: mvn test
Runs 4 jobs (2 versions x 2 OS).
Question 25: How to trigger a workflow manually?
Answer:
on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
default: 'staging'
type: choice
options:
- staging
- production
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to ${{ inputs.environment }}"
Question 26: How to use GitHub environments?
Answer:
jobs:
deploy-staging:
environment: staging
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh ${{ vars.SERVER_URL }}
deploy-production:
needs: deploy-staging
environment:
name: production
url: https://myapp.com
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh ${{ vars.SERVER_URL }}
Environments can have:
- Specific secrets
- Approval rules
- Wait timers
Question 27: How to check code quality?
Answer:
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Check coverage
run: |
mvn jacoco:report
COVERAGE=$(grep -oP 'Total.*?([0-9]+)%' target/site/jacoco/index.html | grep -oP '[0-9]+')
if [ "$COVERAGE" -lt 70 ]; then exit 1; fi
Question 28: How to deploy to a server?
Answer:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /app
docker compose pull
docker compose up -d
Question 29: How to notify the team of the result?
Answer:
jobs:
notify:
runs-on: ubuntu-latest
needs: [build, test, deploy]
if: always()
steps:
- name: Slack Notification
uses: slackapi/slack-github-action@v1
with:
channel-id: 'deployments'
slack-message: |
Deployment ${{ needs.deploy.result }}
Commit: ${{ github.sha }}
Author: ${{ github.actor }}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_TOKEN }}
Question 30: What are the best CI/CD practices?
Answer:
- Fail fast: stop as soon as a test fails
- Aggressive caching: Maven dependencies, Docker images
- Parallelize: independent tests in parallel
- Secure secrets: never in plain text
- Separate environments: staging before production
- Automatic rollback: in case of failure
- Notifications: Slack, email for failures
- Artifacts: save test reports
- Semantic versioning: tags for releases
- Branch protection: mandatory reviews
Score
| Score | Level |
|---|---|
| 27-30 | Excellent - Mastered Docker and CI/CD |
| 21-26 | Good - Some revisions needed |
| 15-20 | Average - Review the chapters |
| Less than 15 | Needs improvement |