Master Dockerfile: Core Commands, Multi‑Stage Builds, and Handy Tricks
This guide explains Dockerfile fundamentals, covering essential instructions like FROM, RUN, COPY, WORKDIR, ENV, EXPOSE, CMD, and ENTRYPOINT, shows a complete example, demonstrates multi‑stage builds to shrink images, and shares practical tips for reliable builds.
Dockerfile is a plain‑text script that defines how to build a Docker image by specifying each step and configuration.
Key Functions
1. Define a base image
FROM ubuntu:22.04</code><code># or any other base such as openjdk:11-jdk2. Install dependencies using RUN to execute shell commands.
RUN apt-get update && apt-get install -y \
python3 \
pip \
curl3. Copy local files into the image with COPY.
COPY ./app /app</code><code>COPY requirements.txt /app/4. Set the working directory with WORKDIR, which creates the directory if it does not exist. WORKDIR /app Relative paths can be chained, e.g.,
WORKDIR /app
WORKDIR src # resolves to /app/src
WORKDIR ../data # resolves to /app/data5. Define environment variables using ENV.
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk
ENV PATH=$PATH:$JAVA_HOME/bin6. Expose ports with EXPOSE. EXPOSE 8080 7. Specify the start command using CMD (overridable) or ENTRYPOINT (fixed).
CMD ["python3", "app.py"]
# or
ENTRYPOINT ["java", "-jar", "app.jar"]Both can be combined; for example, an Nginx image:
FROM nginx
ENTRYPOINT ["nginx", "-g"]
CMD ["daemon off;"]Basic Dockerfile Example
# 1. Base image
FROM ubuntu:22.04
# 2. Maintainer (optional)
LABEL maintainer="[email protected]"
# 3. Environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk
# 4. Install dependencies
RUN apt-get update && \
apt-get install -y \
openjdk-11-jdk \
maven && \
rm -rf /var/lib/apt/lists/*
# 5. Set work directory
WORKDIR /app
# 6. Copy source files
COPY pom.xml .
COPY src ./src
# 7. Build the application
RUN mvn clean package -DskipTests
# 8. Expose port
EXPOSE 8080
# 9. Start the app
CMD ["java", "-jar", "target/app.jar"]Multi‑Stage Build (Reduce Image Size)
# Build stage
FROM maven:3.9-eclipse-temurin-11 AS builder
WORKDIR /build
COPY . .
RUN mvn clean package
# Runtime stage (only runtime artifacts)
FROM eclipse-temurin:11-jre
WORKDIR /app
COPY --from=builder /build/target/app.jar .
CMD ["java", "-jar", "app.jar"]Common Docker Commands
# Build the image
docker build -t my-app:1.0 .
# Run a container
docker run -p 8080:8080 my-app:1.0
# List images
docker images
# View build history
docker history my-app:1.0Practical Trick
Append || true to rm commands to prevent build failures when the file does not exist.
# Without || true – build fails if file is missing
RUN rm /opt/spark/jars/arrow-c-data-15.0.0.jar
# Error: rm: cannot remove ...: No such file or directory
# With || true – command always succeeds
RUN rm /opt/spark/jars/arrow-c-data-15.0.0.jar || trueIn summary, Dockerfile enables automated, reproducible image creation, ensures consistent environments, simplifies deployment, supports version control, and can be optimized with multi‑stage builds and small reliability tricks.
Big Data Technology Tribe
Focused on computer science and cutting‑edge tech, we distill complex knowledge into clear, actionable insights. We track tech evolution, share industry trends and deep analysis, helping you keep learning, boost your technical edge, and ride the digital wave forward.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
