Dockerfile
Dockerfile Explained (With Best Practices & Scenarios)
1. Basic Structure of a Dockerfile
# Base image
FROM ubuntu:20.04
# Maintainer information (optional)
LABEL maintainer="[email protected]"
# Set working directory
WORKDIR /app
# Copy application files
COPY . /app
# Install dependencies
RUN apt-get update && apt-get install -y curl
# Default command
CMD ["echo", "Hello, Docker!"]2. Key Dockerfile Instructions
2.1 FROM (Base Image)
FROM (Base Image)2.2 WORKDIR (Working Directory)
WORKDIR (Working Directory)2.3 COPY vs ADD
COPY vs ADDFeature
COPY
ADD
2.4 RUN (Execute Commands)
RUN (Execute Commands)2.5 CMD vs ENTRYPOINT
CMD vs ENTRYPOINTFeature
CMD
ENTRYPOINT
2.6 EXPOSE (Ports)
EXPOSE (Ports)2.7 VOLUME (Persistent Storage)
VOLUME (Persistent Storage)2.8 ENV (Environment Variables)
ENV (Environment Variables)2.9 HEALTHCHECK (Container Health Monitoring)
HEALTHCHECK (Container Health Monitoring)3. Reducing Docker Image Size
4. Multi-Stage Dockerfile (Best for Production)
5. Best Practices for Writing Dockerfiles
6. Scenario-Based Questions on Dockerfile
Q1: How would you reduce the size of a Docker image?
Q2: When would you use ENTRYPOINT over CMD?
ENTRYPOINT over CMD?Q3: What happens if you specify both CMD and ENTRYPOINT?
CMD and ENTRYPOINT?Q4: How do you prevent unnecessary files from being copied into the image?
7. Summary
Topic
Key Takeaways
Last updated