Docker  

Master Dockerfile Instructions: FROM, WORKDIR, COPY, RUN, EXPOSE, ENTRYPOINT

Here’s a simple and clear explanation of each important Dockerfile instruction with easy-to-remember examples:

🏗️ 1. FROM

Purpose: Sets the base image for your Docker image.

Example

FROM mcr.microsoft.com/dotnet/aspnet:8.0

✔️ Think: "Start from this base image first."

📁 2. WORKDIR

Purpose: Sets the working directory (like cd in Linux).

Example

WORKDIR /app

✔️ Think: "Go to the /app folder before running commands."

📋 3. COPY

Purpose: Copies files/folders from your computer to the Docker image.

Example

COPY . .

✔️ Think: "Copy everything from my project folder into the container's /app (if WORKDIR is set)."

⚙️ 4. RUN

Purpose: Runs commands during build time (e.g., install packages).

Example

RUN dotnet restore

✔️ Think: "Run this command when building the image."

🌐 5. EXPOSE

Purpose: Tells Docker which port the app inside the container will use.

Example

EXPOSE 5000

✔️ Think: "This app listens on port 5000."

🚪 6. ENTRYPOINT

Purpose: Defines the main starting command when the container runs.

Example

ENTRYPOINT ["dotnet", "MyApp.dll"]

✔️ Think: "When this container starts, run this command."

🎯 Small Complete Example (to remember easily)

FROM mcr.microsoft.com/dotnet/aspnet:8.0   # Use .NET base image
WORKDIR /app                               # Go to /app folder
COPY . .                                   # Copy all files into /app
RUN dotnet restore                         # Restore packages during build
EXPOSE 5000                                # App will use port 5000
ENTRYPOINT ["dotnet", "MyApp.dll"]         # Start the app when container runs

Quick Memory Trick 🧠

F - FROM           (Base image)
W - WORKDIR        (Set working folder)
C - COPY           (Copy files into image)
R - RUN            (Run build-time command)
E - EXPOSE         (Open a port)
E - ENTRYPOINT     (Start the app)

FWCREE – Just remember this order. 🔑