wasAsked.

What is a Dockerfile and what are its most common instructions?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Docker

Community answers

Posted by wasAsked editorial collection

A Dockerfile is a text file containing step-by-step instructions to build a Docker image. Each instruction creates a layer, and Docker caches layers to make rebuilds faster. Example: ASP.NET Core API (multi-stage) # Build stage FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build WORKDIR /src COPY ["Orders.Api/Orders.Api.csproj", "Orders.Api/"] RUN dotnet restore "Orders.Api/Orders.Api.csproj" COPY . . RUN dotnet publish "Orders.Api/Orders.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false # Runtime stage FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final WORKDIR /app COPY --from=build /app/publish . USER app EXPOSE 8080 ENTRYPOINT ["dotnet", "Orders.Api.dll"] Build and run: docker build -t orders-api:1.0 . docker run -p 8080:8080 orders-api:1.0 Most common instructions - FROM: sets the base image; every Dockerfile starts with it (except ARG before FROM). Use specific tags, not latest. - WORKDIR: sets the working directory for following instructions (creates it if needed). - COPY: copies files from the build context into the image. COPY --from=stage copies from an earlier build stage. - ADD: like COPY but can also extract local tar archives and fetch URLs. Prefer COPY unless you need these features. - RUN: executes a command during the build (installing packages, restoring dependencies, compiling). Each RUN creates a layer; chain related commands and clean up caches in the same layer. - ENV: sets environment variables available at build and runtime (ENV ASPNETCORE_URLS=http://+:8080). - ARG: defines build-time variables (ARG BUILD_CONFIGURATION=Release); not available at runtime unless copied into ENV. Do not pass secrets with ARG — they can leak into image history; use BuildKit secrets (RUN --mount=type=secret). - EXPOSE: documents the port the container listens on (does not publish it; use -p at runtime). - USER: sets the user for running subsequent commands and the container — run as non-root for security. - CMD: default command or arguments when the container starts; can be overridden by docker run arguments. - ENTRYPOINT: the main executable of the container. CMD often supplies default arguments to ENTRYPOINT. Prefer the exec (JSON array) form so signals reach the process correctly. - HEALTHCHECK: defines a command Docker uses to check container health. - LABEL: metadata such as version, maintainer or source repository. - VOLUME: declares a mount point for persistent or shared data. - SHELL: changes the default shell for RUN commands (useful on Windows images). Best practices - Order instructions from least to most frequently changing: copy project files and restore dependencies before copying the rest of the source, so dependency layers stay cached. - Use multi-stage builds to keep SDKs and build tools out of the final image. - Use small, trusted base images (runtime-only, Alpine, distroless or chiseled images) and pin versions. - Add a .dockerignore file to exclude bin, obj, node_modules, .git and secrets from the build context. - Run as a non-root user and avoid storing secrets in images. - Scan images for vulnerabilities and rebuild regularly to pick up base image patches. Note: .NET can also build container images without a Dockerfile using dotnet publish /t:PublishContainer, which applies many of these best practices automatically.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics