Master Dockerfile: Essential Commands and Real‑World Image Builds
This guide explains Dockerfile fundamentals, walks through each instruction such as FROM, RUN, COPY, and ENTRYPOINT, and provides step‑by‑step examples for building Nginx and Tomcat images, helping readers create reliable container images from scratch.
Dockerfile Basics
A Dockerfile is a plain‑text script that lists instructions Docker uses to assemble an image. During docker build, Docker reads the file line by line, creates a new layer for each command, and produces a final image.
Key Instructions and Their Usage
The most common Dockerfile directives include:
FROM – specifies the base image, e.g., FROM centos.
MAINTAINER – records the image author.
RUN – executes commands while building the image. It supports shell form ( RUN yum install -y wget) and exec form ( RUN ["/bin/bash","-c","echo hello"]).
COPY – copies files from the build context into the image, e.g., COPY index.html /usr/share/nginx/html/.
ADD – similar to COPY but can automatically unpack compressed archives.
EXPOSE – declares the ports the container listens on; it does not publish them.
CMD – provides a default command that runs when the container starts, which can be overridden at docker run time.
ENTRYPOINT – defines the executable that always runs; arguments supplied at docker run are passed to it.
VOLUME – creates an anonymous data volume to persist data.
WORKDIR – sets the working directory for subsequent instructions.
ENV – defines environment variables available during build and at runtime.
USER – switches the user for following commands.
ONBUILD – registers trigger instructions that execute when the image is used as a base for another build.
LABEL – adds metadata to the image.
HEALTHCHECK – defines a command to test container health.
ARG – declares build‑time variables.
Example: Building an Nginx Image
Below is a complete Dockerfile that installs Nginx on a CentOS base, copies a custom index.html, exposes port 80, and sets an ENTRYPOINT to start Nginx.
FROM centos
MAINTAINER xianchao
RUN yum install -y wget nginx
COPY index.html /usr/share/nginx/html/
EXPOSE 80
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]Build and run the image:
docker build -t="dockerfile/nginx:v1" .
docker run -d -p 80:80 --name mynginx dockerfile/nginx:v1After the container starts, accessing the host’s port 80 returns the custom HTML page.
Example: Building a Tomcat Image
This Dockerfile demonstrates adding a JDK RPM and a Tomcat tarball, extracting them, exposing port 8080, and configuring an ENTRYPOINT that starts Tomcat and keeps the container alive.
FROM centos
MAINTAINER xianchao
RUN yum install -y wget
ADD jdk-8u45-linux-x64.rpm /usr/local/
ADD apache-tomcat-8.0.26.tar.gz /usr/local/
RUN cd /usr/local && rpm -ivh jdk-8u45-linux-x64.rpm
RUN mv /usr/local/apache-tomcat-8.0.26 /usr/local/tomcat8
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat8/bin/startup.sh && tail -F /usr/local/tomcat8/logs/catalina.outBuild and run:
docker build -t="tomcat8:v1" .
docker run -d -p 8080:8080 --name mytomcat tomcat8:v1Connecting to the host’s port 8080 reaches the Tomcat welcome page, confirming the image works as intended.
Best Practices
When writing Dockerfiles, prefer COPY over ADD unless you need automatic archive extraction. Keep the number of layers low by chaining related commands with &&. Use ARG for build‑time variables and ENV for runtime configuration. Remember that only the last ENTRYPOINT in a Dockerfile takes effect.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
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.
