Cloud Native 11 min read

Master Dockerfile: Build Custom Images Step‑by‑Step

This tutorial explains Dockerfile fundamentals, key instructions, how to build images with docker build, and provides a complete example of creating an SSHD server image, covering syntax, best practices, and container execution details.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Dockerfile: Build Custom Images Step‑by‑Step

1. Dockerfile Basic Structure

A Dockerfile consists of four parts: base image information, maintainer information (optional), image operation instructions, and the container start command. Example:

# This dockerfile uses the ubuntu image # VERSION V1.0 # Author: 旺旺 # Base image (must be first non‑comment line) FROM ubuntu # Maintainer information (optional) MAINTAINER Barlow [email protected] # Image operation commands 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 "\ndaemon off;" >> /etc/nginx/nginx.conf # Container start command CMD /usr/sbin/nginx

The first instruction must be FROM. Each RUN creates a new layer; the final CMD specifies the command executed when the container starts.

2. Main Instructions and Usage

FROM

Specifies the base image: FROM <image> or FROM <image>:<tag>. It must be the first instruction and can appear multiple times when building multi‑stage images.

MAINTAINER

Sets maintainer information: MAINTAINER <name>.

RUN

Executes a command during image build. Two forms:

RUN &lt;command&gt; (executed via /bin/sh -c ) RUN ["executable", "param1", "param2"] (exec form). Long commands can be split with \ .

CMD

Defines the default command when the container runs. Three formats:

CMD ["executable","param1","param2"] (exec form, recommended) CMD command param1 param2 (shell form) CMD ["param1","param2"] (default parameters for ENTRYPOINT)

Only one CMD is allowed; the last one wins. Runtime arguments override it.

EXPOSE

Declares ports the container listens on: EXPOSE <port> [<port>...].

ENV

Sets environment variables: ENV <key> <value>. They persist in later layers and at runtime.

ADD

Copies files into the image. Syntax: ADD <src> <dest>. <src> can be a local path, URL, or tar archive (auto‑extracted).

COPY

Similar to ADD but preferred for local files: COPY <src> <dest>. After copying, you may want to adjust permissions with RUN chmod.

ENTRYPOINT

Configures the container’s entry command, which cannot be overridden by docker run arguments. Two forms:

ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2

Only one ENTRYPOINT is allowed per Dockerfile.

VOLUME

Creates a mount point for external storage: VOLUME ["/data"].

USER

Specifies the user (or UID) for subsequent commands: USER daemon.

WORKDIR

Sets the working directory for following instructions: WORKDIR /path/to/workdir. Multiple WORKDIR commands are cumulative.

ONBUILD

Defines instructions that run when the built image is used as a base for another image: ONBUILD <INSTRUCTION>.

3. Building an Image from a Dockerfile

After writing a Dockerfile, run:

docker build [options] PATH

The command reads the Dockerfile in PATH, sends the context to the Docker daemon, and builds the image layer by layer. Use .dockerignore to exclude files from the context.

Example to tag an image:

docker build -t centos:sshd .

The build process repeats RUN, ADD, etc., creating a new layer for each instruction.

4. Example: Building an SSHD Server Image

Dockerfile:

# cat Dockerfile FROM centos:6.6 MAINTAINER Barlow ADD selinux/config /etc/selinux/config RUN yum -y install openssh openssh-server RUN sed -i 's/\(session.*\)required\(.*pam_loginuid.so\)/\1optional\2/' /etc/pam.d/sshd RUN /etc/init.d/sshd start RUN echo "root:password" | chpasswd EXPOSE 22 CMD /usr/sbin/sshd -D

Directory listing shows a selinux folder containing the configuration file.

# ls -l total 4 -rw-r--r-- 1 root root 299 Oct 30 Dockerfile drwxr-xr-x 2 root root 19 Oct 30 selinux

Build the image:

# docker build -t centos:sshd .

Run and test the container:

# docker run -d -p 2222:22 centos:sshd # docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 1a91a673297c centos:sshd "/bin/sh -c '/usr/sb..." 0.0.0.0:2222->22/tcp admiring_curie # ssh -p 2222 127.0.0.1
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.

DevOpscontainerizationDockerfileImage Build
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.