Docker front-end and back-end deployment process (background three kinds - foreground one)

backend deployment

One: Idea submitted to mirror warehouse/server -> docker deployment

1. Add Dockerfile at pom level

FROM eclipse-temurin:8u345-b01-jdk-centos7

LABEL org.opencontainers.image.author="author"

RUN mkdir -p /gateway

WORKDIR /gateway

ARG JAR_FILE=target/gateway.jar

COPY ${JAR_FILE} app.jar

EXPOSE 9999

ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"

CMD java -jar app.jar ${JAVA_OPTS}

2. Supplement the Docker configuration in the local pom file

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>${docker.plugin.version}</version>
    <configuration>
        <!-- Docker Remote Api-->
        <dockerHost>${docker.host}</dockerHost>
        <!-- Docker 镜像私服-->
        <registry>${docker.registry}</registry>
      <!--                  <pushRegistry>${docker.registry}</pushRegistry>-->
        <!-- 认证信息-->
        <authConfig>
            <push>
                <username>${docker.username}</username>
                <password>${docker.password}</password>
            </push>
        </authConfig>
        <images>
            <image>
                <!-- 镜像名称: 172.17.0.111/library/pig-gateway:2.6.3-->
                <name>${docker.registry}/${docker.namespace}/${project.name}:${project.version}</name>
                <build>
                    <dockerFile>${project.basedir}/Dockerfile</dockerFile>
                </build>
            </image>
        </images>
    </configuration>
</plugin>
// 此方法为直接推送到服务器 镜像名称为:registry/namespace/gateway
<properties>
  <docker.plugin.version>0.32.0</docker.plugin.version>
  <docker.host>http://192.168.1.196:5678</docker.host>
  <docker.registry>registry</docker.registry>
  <docker.namespace>namespace</docker.namespace>
</properties>

// 此方法为直接推送到服务器及docker镜像仓库中 镜像名称为:registry/namespace/gateway
<properties>
  <docker.plugin.version>0.32.0</docker.plugin.version>
  <docker.host>http://192.168.1.196:5678</docker.host>
  <docker.registry>registry</docker.registry>
  <docker.namespace>namespace</docker.namespace>
  <docker.username>admin</docker.username>
  <docker.password>admin123</docker.password>
</properties>

docker.host: The provided docker environment -> docker build will be directly saved to this server

3. Modify the connection information of the production nacos warehouse (production/test/grayscale)

image.png

spring:
  cloud:
    nacos:
      username: prod # TODO 根据实际情况修改
      password: prod # TODO 根据实际情况修改
      discovery:
        server-addr: nacos-server-01:8848 nacos-server-02:8848 nacos-server-03:8848
        namespace: 2485a6bf-e71e-45fa-99c1-367828c74b53 # TODO 根据实际情况修改
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        namespace: 2485a6bf-e71e-45fa-99c1-367828c74b53 # TODO 根据实际情况修改

3. Local project compiles and loads code

image.png

4. Docker locally packages and submits the image

image.png

5. The server checks whether the docker account contains images

Startup method 1: docker run method

# 服务器登录docker:
docker login --username=888888@126.com registry.cn-hangzhou.aliyuncs.com  -p mima1234
# 密码
password=mima1234
# 查看镜像是否存在:
docker images
# ==========直接启动方式================
docker run -d -p 9999:9999 --name test xxxx
docker run:基于镜像启动一个容器
-d:后台方式启动
-p 9090:80: 端口映射,将宿主机的9090端口映射到容器的80端口
--name:容器名
xxxx:要启动的镜像名称

#=========第二种 指定容器内的环境变量 启动两台======================

#启动镜像
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0
--name表示镜像启动后的容器名称  
-d: 后台运行容器,并返回容器ID;
-e: 指定容器内的环境变量
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口

Start mode 2:

#准备:docker-compose.yml
#在包含compose文件夹中启动容器 docker compose up 容器 -d
docker compose up gateway -d

docker-compose.yml

version: '3'
services:

  gateway:
    image: gateway
    restart: always
    extra_hosts:
      - mysql:172.21.199.54
      - register:172.21.199.54
    ports:
      - "9999:9999"
    container_name: gateway
    hostname: gateway

docker-compose.yml

2. The server pulls the code and deploys it directly

1. Find the code storage directory -> git pulls the new submitted code

git pull

2. Maven compiles the project

mvn clean install -Dmaven.test.skip=true -Ptest

3. Create a new image and container (the code contains the dockerfile file)

docker compose build gateway

image.png

4. Start the container (the folder contains docker-compose.yml)

docker compose up gateway -d

3. Idea deployment

Front-end deployment process

1. Extranet project deployment process

1. Preparation materials:

Put the following (except nginx image) into a new folder

  1. front-end dist package
  2. default.conf configuration file
  3. docker-compose.yml configuration
  4. Dockerfile packaging configuration
  5. nginx mirror
default.conf

Note:
proxy_pass http://192.168.1.166:9999;
address must be a hardcoded server address

server {
    
    
    listen 80;
    server_name localhost;

    client_max_body_size 5M;

        location / {
    
    
                root /usr/share/nginx/html;
                index index.html;
                try_files $uri $uri/ /sys/$uri /index.html;
        }

    # 后端服务入口:注意维护新增微服务,gateway 路由前缀
    location ~* ^/(code|auth|admin|gen|inst|order|project) {
    
    
       proxy_pass http://192.168.1.166:9999;
       #proxy_set_header Host $http_host;
       proxy_connect_timeout 60s;
       proxy_send_timeout 60s;
       proxy_read_timeout 60s;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto http;
    }
    # 避免端点安全问题
    if ($request_uri ~ "/actuator"){
    
    
        return 403;
    }

}

docker-compose.yml
version: '3'
services:

  admin-ui:
    image: admin-ui #镜像名称
    privileged: true
    restart: always
    container_name: admin-ui #容器名称
    networks:
      - pig_default
    external_links:
      - pig-gateway
    ports:
      - 80:80

# 加入到后端网络, 默认为 pig_default  | docker network ls   查看
networks:
  pig_default:
    external: true

Dockerfile

Note:
FROM admin-nginx:latest
must be the nginx mirror name

FROM admin-nginx:latest
MAINTAINER admin # 维护者信息

# 将项目根目录下dist文件夹下的所有文件复制到镜像中 /usr/share/nginx/html/ 目录下
COPY admin-ui/ /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/default.conf


2. Start deployment

1. Enter the folder

cd /home/hc
image.png

2. Package the front-end image

docker build -t admin-ui .
image.png

3. Start the container

docker run -d -p 80:80 --name admin-ui admin-ui
image.png

4. Start successfully

Reference: Docker image + Nginx configuration deployment Vue project

2. Mirror version deployment process:

Reference: Mirror packaging

1. The original server image packaging:

1. Prepare the images and folders that need to be packaged (refer to the first step of the external network deployment process)
2. Package: docker save -o admin-ui.tar admin-ui (deployment through nginx needs to package nginx)

2. Just put the packaged image on the new server

image.png
By decompressing the image command docker load <

3. Mirror deployment
image.png

Guess you like

Origin blog.csdn.net/weixin_44824381/article/details/130385072