Cloud Native 12 min read

Master Docker Images & Containers: Build, Run, and Deploy with Dockerfile

This comprehensive guide explains Docker image fundamentals, the UnionFS layering model, image creation with Dockerfile, committing containers, using volumes (including MySQL and permission flags), building custom CentOS and Tomcat images, uploading to Alibaba Cloud Registry, and deploying SpringBoot applications, all with concrete commands and examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Docker Images & Containers: Build, Run, and Deploy with Dockerfile

What Is a Docker Image?

A Docker image is a lightweight, executable software package that bundles an application’s code, runtime libraries, environment variables, and configuration files into a single, portable unit.

How Docker Images Are Loaded – UnionFS

Docker uses a Union File System (UnionFS) to layer multiple read‑only filesystems on top of each other. The lowest layer, bootfs , contains the bootloader and kernel. Above it, rootfs provides the typical Linux directories (/dev, /proc, /bin, /etc, etc.). Each additional layer adds or modifies files, and the combined view appears as a single filesystem when a container runs.

Key Characteristics of Docker Images

All image layers are read‑only.

When a container starts, Docker adds a writable layer on top of the image layers; this is the container layer.

Committing a Container to an Image

docker commit -a="test" -m="test" <container_id> tomcat01:1.0

Docker Volumes

What Is a Volume?

Volumes enable data sharing between containers and synchronization of container‑generated data to the host.

Creating and Using Volumes

docker run -it -v /host/dir:/container/dir /bin/bash

Mounting a MySQL Database to the Host

# 1. Pull MySQL image
docker pull mysql
# 2. Run MySQL with mounted directories and set root password
docker run -d -p 3344:3306 \
  -v /home/conf:/etc/mysql/conf.d \
  -v /home/logs:/logs \
  -v /home/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 mysql

If remote clients cannot connect, adjust MySQL permissions inside the container:

# Enter the container
docker exec -it <container_id> /bin/bash
# Log into MySQL
mysql -uroot -p123456
# Grant remote access
GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
# Change authentication method
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;

Named and Anonymous Mounts

-v /container/path                # anonymous mount
-v volume_name:/container/path    # named mount
-v /host/path:/container/path    # bind mount (host directory)

Binding Permissions

# Read‑only mount
docker run -d -v myvol:/etc/nginx:ro nginx
# Read‑write mount
docker run -d -v myvol:/etc/nginx:rw nginx

Data‑Volume Container

Use a dedicated container to share data between multiple containers (e.g., sharing MySQL data):

docker run -d --name mysql02 -p 3345:3306 \
  --volumes-from mysql01 \
  -e MYSQL_ROOT_PASSWORD=123456 mysql

Dockerfile Basics

A Dockerfile is a script of instructions used to build a Docker image.

FROM        # Base image
MAINTAINER  # Author information
RUN         # Commands executed during build
ADD         # Add files or archives
COPY        # Copy files (similar to ADD)
WORKDIR     # Set working directory
VOLUME      # Declare mount point
EXPOSE      # Document port to expose
ENV         # Set environment variables
CMD         # Default command (last one wins)
ENTRYPOINT  # Command that cannot be overridden without --entrypoint

Building a Custom CentOS Image

Create a Dockerfile (e.g., vim mycentos).

Write the following content:

FROM centos
MAINTAINER MT<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
EXPOSE 80
CMD /bin/bash

Build the image: docker build -f mycentos -t mycentosdemo:1.0 . View the image history:

docker history <image_id>

Creating a Tomcat Image

Prepare the required archives: Tomcat and JDK tarballs.

Write a Dockerfile:

FROM centos
MAINTAINER fortuneteller<[email protected]>
COPY README.txt /usr/local/README.txt
ADD jdk-8u251-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.35.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_251
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.35
EXPOSE 8080
CMD ["/usr/local/apache-tomcat-9.0.35/bin/catalina.sh","run"]

Build the image: docker build -t mytomcat . Run the container with volume mounts for webapps and logs:

docker run -d -p 3344:8080 \
  --name mttomcat \
  -v /home/fortuneteller/tomcat/test:/usr/local/apache-tomcat-9.0.35/webapps/test \
  -v /home/fortuneteller/tomcat/logs:/usr/local/apache-tomcat-9.0.35/logs \
  mytomcat
Tomcat container test result
Tomcat container test result

Uploading a Local Image to Alibaba Cloud Container Registry

Log in to Alibaba Cloud Container Registry.

Create a namespace.

Create a repository.

Set a fixed password for the repository.

Obtain the push command from the console.

Push the image:

docker tag mytomcat registry.cn-hangzhou.aliyuncs.com/your_namespace/mytomcat:1.0
docker push registry.cn-hangzhou.aliyuncs.com/your_namespace/mytomcat:1.0

Pull the image on another host using the provided pull command.

Deploying a SpringBoot Application with Docker

FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
# Build the image
docker build -t ideatest .
# List images
docker images
# Run the container
docker run -d -p 3344:8080 ideatest  # Ensure port 3344 is open in Alibaba Cloud security group

Access the application at http://<server_ip>:3344.

Docker Summary

Docker provides a layered, union‑filesystem based image model, read‑only image layers with a writable container layer, flexible volume mechanisms for persistent data, and a declarative Dockerfile syntax for reproducible builds. Combined with cloud registries such as Alibaba Cloud, Docker enables rapid packaging, distribution, and deployment of services ranging from simple static sites to complex SpringBoot applications.

Docker overall diagram
Docker overall diagram
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.

DockerSpringBootDockerfileAlibaba CloudContainersVolumesImage Building
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.