Cloud Native 13 min read

Master Docker: Build, Run, and Deploy Custom Images with Real-World Examples

This guide walks through Docker’s workflow—image building with Dockerfiles, container startup, and service orchestration with docker‑compose—providing step‑by‑step examples for creating nginx, PHP, Tomcat, Jenkins, and a full LNMP stack, plus essential commands and configuration tips.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Docker: Build, Run, and Deploy Custom Images with Real-World Examples

Docker’s usage process consists of two main stages: image building and container startup .

Dockerfile defines the image build process. By writing a Dockerfile that specifies the required environment, dependencies, and application code, you can create an image with docker build .. Docker reads the Dockerfile from top to bottom, executing each instruction to assemble the image.

docker‑compose.yml records the build and runtime configuration for a multi‑container project, allowing you to manage relationships, network ports, volumes, and whether to use official images or custom builds.

1. dockerfile: build image
2. docker run: start container
3. docker‑compose: start services

Common Dockerfile commands include:

docker build .                     # build image in current directory
docker build -t shykes/myapp .    # tag the image
docker build -t shykes/myapp -f /path/Dockerfile /path   # specify Dockerfile path

Before creating containers, enable kernel IP forwarding to ensure network connectivity:

echo -e "net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0" >> /etc/sysctl.conf
sysctl -p

Example 1: Build an Nginx Image

Goal: Use a CentOS base image to compile and install Nginx from source, adding necessary management tools.

mkdir /root/nginx-dockerfile && cd /root/nginx-dockerfile
cat Dockerfile
FROM centos:7
MAINTAINER zhangfan
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && rm -rf /var/cache/yum/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
    tar zxf nginx-1.15.5.tar.gz && \
    cd nginx-1.15.5 && \
    ./configure --prefix=/usr/local/nginx \
        --with-http_ssl_module \
        --with-http_stub_status_module && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "ok" >> /usr/local/nginx/html/status.html && \
    cd / && rm -rf nginx-1.12.2* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Build and verify:

docker build -t nginx:v1 .   # build image
docker images                # list images
docker run -d --name nginx01 -p 88:80 nginx:v1   # start container
curl http://192.168.106.100:88/status.html   # test

Example 2: Build a PHP Base Image

Create a directory for the PHP Dockerfile and define the build steps.

cat Dockerfile
FROM centos:7
MAINTAINER zhangfan
RUN yum install -y epel-release && yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel libmcrypt-devel libxslt-devel libtidy-devel autoconf \
    iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/yum/*
RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \
    tar zxf php-5.6.36.tar.gz && cd php-5.6.36 && \
    ./configure --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --enable-fpm --enable-opcache \
        --with-mysql --with-mysqli --with-pdo-mysql \
        --with-openssl --with-zlib --with-curl --with-gd \
        --with-jpeg-dir --with-png-dir --with-freetype-dir \
        --enable-mbstring --with-mcrypt --enable-hash && \
    make -j 4 && make install && \
    cp php.ini-production /usr/local/php/etc/php.ini && \
    cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
    sed -i "90a daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    mkdir /usr/local/php/log && cd / && rm -rf php* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin:/usr/local/php/bin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]

Build and run:

docker build -t php:v1 .
docker run -d --name php01 php:v1
docker exec -it php01 bash
bin/php -v   # verify version

Example 3: Build a Tomcat Image

FROM centos:7
MAINTAINER zhangfan
ENV VERSION=8.5.61
RUN yum install -y java-1.8.0-openjdk wget curl unzip iproute net-tools && yum clean all && rm -rf /var/cache/yum/*
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz && \
    tar zxf apache-tomcat-${VERSION}.tar.gz && \
    mv apache-tomcat-${VERSION} /usr/local/tomcat && \
    rm -rf apache-tomcat-${VERSION}.tar.gz && \
    rm -rf /usr/local/tomcat/webapps/* && \
    mkdir /usr/local/tomcat/webapps/test && \
    echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
    sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]

Build and test:

docker build -t tomcat:v1 .
docker run -d --name tomcat01 -p 8089:8080 tomcat:v1
curl http://192.168.106.100:8089/test/status.html   # should show "ok"

Example 4: Build a Jenkins Project on Top of Tomcat

mkdir /root/jenkins-dockerfile && cd /root/jenkins-dockerfile
wget https://get.jenkins.io/war-stable/2.263.1/jenkins.war
cat Dockerfile
FROM tomcat:v1
MAINTAINER zhangfan
COPY jenkins.war /usr/local/tomcat/webapps/ROOT.war
docker build -t tomcat:v2 .
docker run -d --name tomcat02 -p 8888:8080 tomcat:v2
# Access http://192.168.106.100:8888/login?from=%2F to see Jenkins initialization page

Example 5: Quickly Build an LNMP Platform

Create a custom network: docker network create lnmp Create a MySQL container:

docker run -d \
  --name lnmp_mysql \
  --net lnmp \
  --mount src=myql-vol,dst=/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -e MYSQL_DATABASE=wordpress \
  mysql:5.7 --character-set-server=utf8

Create a PHP container:

docker run -d --name lnmp_php --net lnmp \
  --mount src=wwwroot,dst=/wwwroot php:v1

Create an Nginx container (using a custom nginx.conf):

docker run -d --name lnmp_nginx --net lnmp -p 8000:80 \
  --mount type=bind,src=$(pwd)/nginx.conf,dst=/usr/local/nginx/conf/nginx.conf \
  --mount src=wwwroot,dst=/wwwroot nginx:v1

Deploy WordPress:

cd /var/lib/docker/volumes/wwwroot/_data
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar -xf latest-zh_CN.tar.gz
# Access http://192.168.106.100:8000/wordpress

These examples demonstrate how Dockerfiles can automate the deployment of various services, from a simple Nginx web server to a full LNMP stack with WordPress.

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.

DockerDevOpscontainerizationDockerfileDocker Compose
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.