Cloud Native 12 min read

How to Build a Custom Docker Image and Automate Deployment with CI/CD Scripts

This guide walks you through creating a Dockerfile to customize a Docker image, explains why Dockerfile‑based builds are preferred over docker commit, shows step‑by‑step commands for building, tagging, and running the image, and provides a Bash script to automate the full CI/CD lifecycle.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
How to Build a Custom Docker Image and Automate Deployment with CI/CD Scripts

Background

The author plans to migrate an existing project to Docker‑based deployment and already uses a cloud provider's CI/CD pipeline. To integrate CI/CD, they need scripts that handle Docker image customization, container start, and stop.

What Is a Dockerfile?

A Dockerfile is a plain‑text file containing a sequence of instructions (e.g., FROM, RUN, ADD, COPY, CMD, ENV) that define how to build an image. Each instruction creates a new immutable layer; later layers cannot modify earlier ones, so unnecessary files should be removed before the layer finishes.

Why Not Use docker commit ?

Although docker commit can snapshot a running container, it produces a “black‑box” image whose creation steps are undocumented, making maintenance painful and causing image bloat because deleted files remain in previous layers. Therefore, the article recommends using a Dockerfile for reproducible, transparent builds.

Dockerfile Instructions

FROM

Specifies the base image; it must be the first instruction. The example uses FROM java:8, pulling the OpenJDK 8 image from Docker Hub.

docker pull java:8

COPY

Copies files from the build context into the image. The example copies the Spring Boot JAR hqy-service-channel.jar to /app.jar inside the container.

COPY ./hqy-service-channel.jar ./app.jar

ENV

Sets environment variables. Here it defines spring.profiles.active=prod to select the production profile.

ENV spring.profiles.active prod

EXPOSE

Declares the port the container will listen on (8190) to aid users and enable automatic port mapping with docker run -P.

EXPOSE 8190

ENTRYPOINT

Specifies the command that runs when the container starts. The example runs the JAR with timezone configuration.

ENTRYPOINT ["java", "-jar", "-Duser.timezone=GMT+08", "./app.jar"]

RUN (optional)

Executes shell commands during the build. The article mentions the syntax but does not use it in the sample.

Building the Image

After placing the Dockerfile and JAR in the same directory, run: docker build -t channel . The build output shows each step, and the final image channel:latest (≈725 MB) appears in docker images.

Automation Script (start.sh)

A Bash script automates stopping, removing, rebuilding, and restarting the container, making it CI/CD‑friendly.

#!/bin/bash
# Stop container
docker stop channel
echo "停止容器success!"
# Remove container
docker rm channel
echo "移除容器success!"
# Remove image
docker rmi channel
echo "移除镜像success!"
# Build image
docker build -t channel /opt/channel/docker/
echo "制作镜像success!"
# Run container
docker run -d --name channel -p 8190:8190 -v /opt/channel/logs/:/opt/channel/logs/ channel:latest
echo "启动success!"

Running the script creates a container named channel listening on host port 8190. Updating the JAR and re‑executing the script rebuilds and redeploys the service.

Conclusion

The article demonstrates a complete, reproducible workflow for creating a Docker image with a Dockerfile, highlights best practices (layer minimization, avoiding docker commit), and provides a ready‑to‑use script to integrate the process into CI/CD pipelines.

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.

ci/cdDockerfileshell script
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

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.