SpringBoot + Docker implements a build and runs everywhere

1. Benefits of containerized deployment

11a25a7fecea7124f2dd410623a4023a.png

As an emerging virtualization method, Docker can make more efficient use of system resources without requiring additional overhead such as hardware virtualization and running a complete operating system.

Traditional virtual machine technology often takes several minutes to start application services, but Docker container applications can achieve second-level or even millisecond-level startup time because they directly run the host kernel without starting a complete operating system. It greatly saves the time of development, testing and deployment.

The most important thing is a consistent operating environment. The Docker image provides a complete runtime environment except the kernel, ensuring the consistency of the application runtime environment. So as to achieve a real construction once and execute everywhere.

2. Build a mirror

2.1, Dockerfile

Then we started to use Dockerfile to customize our image to achieve the purpose of containerization. The essence of Dockerfile is to integrate a series of modification, installation, construction, and operation commands together to build a personalized image, so as to achieve build once and run everywhere. Next, let’s build a custom image.

Create a docker directory under the my-project-server module, and create a Dockerfile under the docker directory. code show as below:

FROM openjdk:8-jre

MAINTAINER Micromaple <[email protected]>

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 8899
  • FROM: specify the base image, the project is developed using jdk8, so the base image we specify is openjdk:8-jre

  • MAINTAINER: project maintainer

  • RUN: Execute the command to create an app directory under the root directory

  • COPY: Copy the my-project-server-1.0.0-SNAPSHOT.jar file in the current directory of the host to the app directory and rename it to app.jar

  • ENTRYPOINT: Specify the container startup program and parameters

  • EXPOSE: Specify the port where the runtime container provides services

2.2, start building

Create a directory on the virtual machine

mkdir -p /usr/local/docker/my-project/docker

Upload the packaged my-project-server-1.0.0-SNAPSHOT.jar file and Dockerfile to this directory.

4f1e5e7ba419322a6ea3afdfe6b2300e.png

Execute the image build command

docker build -t my-project-server:v1 .

build successfully

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-project-server v1 ed30386b06d2 11 seconds ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB

3. Build a private server

3.1. Build Docker Registry

Create a directory

mkdir -p /usr/local/docker/registry

Create docker-compose.yml of Docker Registry in this directory. The content is as follows:

cd /usr/local/docker/registry
version: '3.1'
services:
  registry:
    image: registry
    restart: always
    container_name: registry
    ports:
      - 5000:5000
    volumes:
      - ./data:/var/lib/registry

Start the container

docker-compose up -d

3.2. Build Docker Registry WebUI

Create a directory

mkdir -p /usr/local/docker/docker-registry-frontend

Create docker-compose.yml of Docker Registry in this directory. The content is as follows:

cd /usr/local/docker/docker-registry-frontend
version: '3.1'
services:
  front end:
    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 (the IP of the Docker warehouse)
      - ENV_DOCKER_REGISTRY_PORT=5000

You need to change the value of ENV_DOCKER_REGISTRY_HOST to the IP of your own DockerRegistry service

Start the container

docker-compose up -d

Browser access http://192.168.110.158:8080/, the effect is as follows:

5a62988094b04f5ff175e4332fc46128.png

3.3, client configuration

The client that needs to upload the Docker image needs to configure daemon.json, the full path is in /etc/docker/daemon.json

vi /etc/docker/daemon.json

The following content needs to be added:

"insecure-registries": [
  "IP of Docker repository: 5000"
]

Complete daemon.json content:

{
  "registry-mirrors": [
    "https://xxx.mirror.aliyuncs.com"
  ],
  "insecure-registries": [
    "192.168.110.158:5000"
  ]
}
  • registry-mirrors: It is the mirror acceleration address, here is the acceleration address I applied for myself. You can apply for one yourself, or you can find one online.

  • insecure-registries: IP of the Docker repository.

restart service

systemctl daemon-reload
systemctl restart docker

3.4, upload private server

After the construction and configuration are completed, we need to upload the image we built before to our own Docker warehouse.

view mirror

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-project-server v1 6af7d633afb7 5 seconds ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB

1), mirror mark

Use the docker tag to mark the image my-project-server:v1 as 192.168.110.158:5000/my-project-server:v1.

192.168.110.158 is the IP of my Docker warehouse.

The format is: docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]

The command is as follows:

docker tag my-project-server:v1 192.168.110.158:5000/my-project-server:v1

After tagging, view the mirror

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.158:5000/my-project-server v1 6af7d633afb7 3 minutes ago 334MB
my-project-server v1 6af7d633afb7 3 minutes ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB

2), mirror image upload

Use docker push to upload the image.

$ docker push 192.168.110.158:5000/my-project-server
The push refers to repository [192.168.110.158:5000/my-project-server]
5b9e874b9f9c: Pushed
e87c042d22f8: Pushed
b4cfcb8385a8: Pushed
2b730cf18c09: Pushed
edeaba958753: Pushed
8bf42db0de72: Pushed
31892cc314cb: Pushed
11936051f93b: Pushed
v1: digest: sha256:5c8a0efff409649a389d0bc74dda94ca96e67e87c92673b4c7dad0078657af40 size: 2000

3), view mirror

Use curl to view the 192.168.110.158:5000/v2/_catalog address, and you can see the successfully uploaded image

$ curl 192.168.110.158:5000/v2/_catalog
{"repositories":["my-project-server"]}

Here we can successfully see the image we uploaded.

We can also visually view our image on the WebUI we just built. The renderings are as follows:

d89dc82fe4a199df168a2133e99b2f69.png

4), verification

At this point we delete the local mirror and try to download the mirror from the private warehouse

Delete the existing mirror first

docker rmi my-project-server:v1 192.168.110.158:5000/my-project-server:v1

download mirror

docker pull 192.168.110.158:5000/my-project-server:v1
$ docker pull 192.168.110.158:5000/my-project-server:v1
v1: Pulling from my-project-server
0e29546d541c: Already exists
9b829c73b52b: Already exists
cb5b7ae36172: Already exists
99ce012bef04: Already exists
22dc2a72d098: Already exists
9c69a57e10d9: Already exists
776f54050ab5: Pull complete
65a83a9a7871: Pull complete
Digest: sha256:5c8a0efff409649a389d0bc74dda94ca96e67e87c92673b4c7dad0078657af40
Status: Downloaded newer image for 192.168.110.158:5000/my-project-server:v1
192.168.110.158:5000/my-project-server:v1
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.158:5000/my-project-server v1 6af7d633afb7 15 minutes ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB

We can see that it can be pulled normally.

4. Container startup

After the image is built and uploaded, you can directly use Docker Compose to start the container. Build once and run everywhere.

Create a directory

mkdir -p /usr/local/docker/my-project

Create docker-compose.yml of Docker Registry in this directory. The content is as follows:

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/Shanghai

Start the container

docker-compose up -d

View container startup status

docker ps -a

Access to query all user interfaces

  • http://ip:8899/sys-user/get/all

66888331992a595c5f02e8df32fb5dd1.png

Source: blog.csdn.net/qq_41779565/

article/details/127356651

Back-end exclusive technology group

Build a high-quality technical exchange community, welcome to join the group who are engaged in programming development and technical recruitment HR, and also welcome everyone to share your company’s referral information, help each other, and make progress together!

Civilized speech, mainly focusing on communication technology, job referral, industry discussion

Advertisers do not enter, do not trust private chats, to prevent being cheated

7fe56fe7f9ed0a6f5ec30a5c2d58e491.jpeg

Add me as a friend and pull you into the group
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill tree In-depth research on the function and method of container Collection 118798 people are studying systematically