Master Dockerfile: Essential Commands and Real‑World Image‑Building Examples
This guide explains Dockerfile fundamentals, walks through each core instruction with practical examples, demonstrates building images for simple CentOS containers, SpringBoot applications, and shows how to tag, push, and pull images from a registry, providing clear step‑by‑step commands and explanations.
Dockerfile Overview
A Dockerfile is a plain‑text script that lists instructions for building a Docker image. Each instruction creates a new immutable layer, allowing reproducible packaging of applications.
Core Instructions
FROM – selects the base image for the build.
RUN – executes a command in a temporary container and commits the result as a new layer.
EXPOSE – documents which ports the container will listen on; the ports must be published with -p at runtime.
WORKDIR – sets the working directory for subsequent instructions and for the container’s default start directory.
ENV – defines environment variables that can be referenced later.
ADD – copies files/directories from the build context into the image and can also download remote URLs.
COPY – copies files/directories from the build context into the image (no URL support).
VOLUME – declares a mount point that can be bound to a host directory at runtime.
CMD – provides a default command to run when the container starts; it can be overridden by arguments to docker run.
ENTRYPOINT – defines a command that always runs; it can be overridden only with --entrypoint.
Basic Examples
FROM
Build an image based on the official CentOS image: FROM centos Build and tag the image: docker build -t mycentos_test01 . Verify the image:
docker imagesRUN
Install vim on the CentOS base:
FROM centos
RUN yum install -y vimBuild and run:
docker build -t mycentos_test02 .
docker run -it mycentos_test02Check the installation inside the container:
rpm -qa | grep vimEXPOSE
Expose port 80 for a Tomcat image (the port is only documented; it must be published when running):
FROM tomcat:8.0
EXPOSE 80Run with port mapping:
docker run -p 80:80 mytomcat_test01CMD vs ENTRYPOINT
Default command that can be overridden:
FROM centos
CMD ["echo","hello"]Override at run time: docker run mycentos_test03 echo hello Fixed entrypoint (override only with --entrypoint):
FROM centos
ENTRYPOINT ["echo","hello"]Override entrypoint:
docker run --entrypoint="/bin/sh" mycentos_test04WORKDIR and ENV
Set an environment variable and use it as the working directory:
FROM centos
ENV name centos
WORKDIR /$nameRunning the container lands in /centos:
docker run -it mycentos_test06
pwd # outputs /centosVOLUME
Declare a mount point and bind it to a host directory:
FROM centos
VOLUME /centosRun with a bind mount:
docker run -it -v /opt/centos:/centos mycentos_test07ADD
Copy a local file test.txt into the image root:
FROM centos
ADD test.txt /After building, test.txt appears at / inside the container.
Building a SpringBoot Application Image
Assuming a SpringBoot jar SpringBootDemo-0.0.1-SNAPSHOT.jar has been built with mvn clean package, the Dockerfile can be:
FROM openjdk:8
WORKDIR /test
ADD SpringBootDemo-0.0.1-SNAPSHOT.jar /test
EXPOSE 8080
ENTRYPOINT ["java","-jar"]
CMD ["SpringBootDemo-0.0.1-SNAPSHOT.jar"]Build and run the image:
docker build -t springbootdemo .
docker run -p 8080:8080 springbootdemoThe application becomes reachable at http://<host>:8080.
Pushing the Image to a Registry
Tag the image with the target registry address, log in, and push:
# Tag
docker tag 8d4d7be1d926 registry.cn-hangzhou.aliyuncs.com/test_respository/springbootdemo:1.0.0
# Login
docker login --username=<em>your_user</em> registry.cn-hangzhou.aliyuncs.com
# Push
docker push registry.cn-hangzhou.aliyuncs.com/test_respository/springbootdemo:1.0.0Pull and run on another host:
docker pull registry.cn-hangzhou.aliyuncs.com/test_respository/springbootdemo:1.0.0
docker run -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/test_respository/springbootdemo:1.0.0This workflow demonstrates how to write Dockerfiles, build layered images, configure container runtime behavior, and distribute images via a private registry.
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.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.
