Cloud Native 6 min read

Master Dockerfile: Essential Commands to Build Custom Images

This guide explains Dockerfile fundamentals, breaking down its four sections, providing a complete example, and detailing each core instruction such as FROM, MAINTAINER, RUN, CMD, EXPOSE, ENV, ADD, COPY, ENTRYPOINT, VOLUME, USER, and WORKDIR for building Docker images.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master Dockerfile: Essential Commands to Build Custom Images

Dockerfile is a configuration file used to create new Docker images, similar to deployment scripts like Ant, by executing a series of instructions.

Four Parts of a Dockerfile

Base image information

Maintainer information

Image operation instructions

Commands executed when the container starts

Example

FROM ubuntu
MAINTAINER docker_user [email protected]
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "
daemon off;" >> /etc/nginx/nginx.conf
CMD /usr/sbin/nginx

Explanation

The first line must specify the base image, e.g., FROM ubuntu. The MAINTAINER line defines the author’s name and email. RUN executes system commands during image build. CMD defines the command that runs when the container starts. EXPOSE can be used to declare ports, such as EXPOSE 22.

Main Dockerfile Instructions

FROM

: Format FROM<image> or FROM<image>:<tag>. Must be the first instruction. MAINTAINER: Format MAINTAINER<name> to set maintainer info. RUN: Format RUN <command> or RUN["executable","param1","param2"]. Executes commands in a shell or via exec. CMD: Sets the default command for container start. Supports three forms: CMD["executable","param1","param2"], CMD command param1 param2, and CMD["param1","param2"]. Only one CMD is effective; later ones override earlier ones. EXPOSE: Format EXPOSE <port>[ <port>...]. Declares ports the container listens on. ENV: Format ENV <key> <value>. Sets environment variables for later RUN steps and at runtime. ADD: Format ADD <src> <dest>. Copies files, URLs, or tar archives into the image. COPY: Format COPY <src> <dest>. Copies files or directories from the build context into the image. ENTRYPOINT: Configures the container’s entry point, not overridden by docker run arguments. Two forms: JSON array or shell form. Only one ENTRYPOINT is effective. VOLUME: Format VOLUME ["/data"]. Creates a mount point for external storage. USER: Format USER daemon. Sets the user or UID for subsequent commands. WORKDIR: Format WORKDIR /path/to/workdir. Sets the working directory for following instructions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsContainerDockerfile
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.