Docker Containerization: Building Images, Setting Up a Private Registry, and Deploying with Docker Compose
This tutorial explains the advantages of Docker containerization, shows how to write a Dockerfile, build and tag images, configure a private Docker Registry with Docker‑Compose, push and pull images, and finally run the services using Docker‑Compose, providing a complete end‑to‑end workflow.
1. Benefits of Containerized Deployment
Docker, as a lightweight virtualization technology, utilizes system resources efficiently without the overhead of full hardware virtualization or a complete operating system.
Traditional virtual machines take minutes to start, whereas Docker containers start in seconds or milliseconds because they share the host kernel, dramatically reducing development, testing, and deployment time.
The most important benefit is a consistent runtime environment; Docker images contain the full runtime (except the kernel), ensuring "build once, run anywhere".
2. Building the Image
2.1 Dockerfile
We create a Dockerfile to define a custom image. The Dockerfile combines a series of commands for modifications, installations, and builds, achieving the "build once, run anywhere" goal.
In the my-project-server module, create a docker directory and add the following Dockerfile:
FROM openjdk:8-jre
MAINTAINER Micromaple
RUN mkdir /app
COPY my-project-server-1.0.0-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar", "--spring.profiles.active=prod,druid-prod"]
EXPOSE 8899FROM : base image (openjdk:8-jre).
MAINTAINER : project maintainer.
RUN : create /app directory.
COPY : copy my-project-server-1.0.0-SNAPSHOT.jar to /app/app.jar .
ENTRYPOINT : start the Java application with parameters.
EXPOSE : expose port 8899.
2.2 Start Building
Create a directory on the VM and place the built JAR and Dockerfile there:
mkdir -p /usr/local/docker/my-project/dockerBuild the image:
docker build -t my-project-server:v1 .Verify the image:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-project-server v1 ed30386b06d2 11 seconds ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB3. Setting Up a Private Registry
3.1 Docker Registry
Create the registry directory:
mkdir -p /usr/local/docker/registryCreate docker-compose.yml :
version: '3.1'
services:
registry:
image: registry
restart: always
container_name: registry
ports:
- 5000:5000
volumes:
- ./data:/var/lib/registryStart the registry container:
docker-compose up -d3.2 Docker Registry Web UI
Create the UI directory and its docker-compose.yml :
mkdir -p /usr/local/docker/docker-registry-frontend
cd /usr/local/docker/docker-registry-frontend
version: '3.1'
services:
frontend:
image: konradkleine/docker-registry-frontend:v2
ports:
- 8080:80
volumes:
- ./certs/frontend.crt:/etc/apache2/server.crt:ro
- ./certs/frontend.key:/etc/apache2/server.key:ro
environment:
- ENV_DOCKER_REGISTRY_HOST=192.168.110.158
- ENV_DOCKER_REGISTRY_PORT=5000After editing ENV_DOCKER_REGISTRY_HOST to your registry IP, start the UI:
docker-compose up -dAccess it at http://192.168.110.158:8080/ .
3.3 Client Configuration
Edit /etc/docker/daemon.json to trust the private registry:
{
"registry-mirrors": ["https://xxx.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.110.158:5000"]
}Reload and restart Docker:
systemctl daemon-reload
systemctl restart docker3.4 Uploading the Image
Tag the image for the private registry:
docker tag my-project-server:v1 192.168.110.158:5000/my-project-server:v1Push it:
docker push 192.168.110.158:5000/my-project-serverVerify with curl:
curl 192.168.110.158:5000/v2/_catalog
{"repositories":["my-project-server"]}4. Container Startup
Create a directory for the application and a docker-compose.yml that pulls the image from the private registry:
mkdir -p /usr/local/docker/my-project
cd /usr/local/docker/my-project
version: '3.1'
services:
my_project_server:
image: 192.168.110.158:5000/my-project-server:v1
container_name: my-project-server
restart: always
ports:
- 8899:8899
volumes:
- ./logs:/logs
environment:
TZ: Asia/ShanghaiStart the container:
docker-compose up -dCheck status with docker ps -a and access the service (e.g., http://192.168.110.158:8899/sys-user/get/all ).
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.