Use docker, docker-compose to deploy microservices

1. Use docker deployment

1. Prepare

Here, Redis and nacos have been installed and started in docker, and the "ruoyi-gateway-prod.yml" file has been configured.

Note that if there are connection information such as Redis, MySQL, and nacos in the configuration file, you need to change the IP address to the public network address or intranet address of the server, and the connection will not fail.
insert image description here

2. Upload the jar package

Here I take the gateway service of the micro-service version Ruoyi as an example and upload it to the specified directory.

insert image description here

3. Write dockerfile

Official documentation:

https://docs.docker.com/engine/reference/builder/

English seems a bit difficult, Ruoyi has a Chinese tutorial here

insert image description here

This is Zoe's document address:

http://doc.ruoyi.vip/ruoyi-cloud/cloud/dokcer.html#dockerfile

First obtain the basic image needed to build the image, that is, the jdk image, and first look at the jdk images

docker search openjdk

insert image description here
Here is the image of jdk8

docker pull openjdk:8-jre

insert image description here
I've fetched it before, it might be a bit slower the first time.

Then create a file named dockerfile, the file name can be customized, generally it is dockerfile, the content is as follows:
insert code snippet here

# 基础镜像
FROM  openjdk:8-jre
# author
LABEL author="gan"

# 创建目录
RUN mkdir -p /home/jar
# 指定路径,后面运行的jar包就是在这个目录
WORKDIR /home/jar
# 挂载目录
VOLUME /home/jar

# 复制jar文件到路径,第一个是宿主机文件目录(dockerfile所在目录,不能是绝对路径),第二个是容器文件目录
COPY ./ruoyi-gateway.jar /home/jar/ruoyi-gateway.jar

# 启动网关服务jar包
ENTRYPOINT ["java","-jar","-Xms256M","-Xmx256M","ruoyi-gateway.jar"]

Upload to the same directory as the jar package

insert image description here

If you want to copy multiple files to the container, you can do this

insert image description here

3. Build images and containers

Build the mirror image in the current directory. Note that the following "." is not redundant. The command is as follows:

docker build -f /www/wwwroot/changjing/jar/gateway/dockerfile -t ruoyi-gateway .

explain:

-f: Followed by the path of the dockerfile
-t: Label the built image, which is the name of the built image

Notice:

The command to execute the build image must be in the directory where the dockerfile is located, because the "COPY" command of the dockerfile can only be in a relative directory relative to the dockerfile, and an error will be reported if the absolute directory of the host is used to build.

The execution results are as follows:

insert image description here

The build was successful, because I have already built it successfully before, and it may be different from the first build.

Next start the mirror, the command is as follows:

docker run -d --name ruoyi-gateway -p 8010:8010 ruoyi-gateway

explain:

–name: followed by the container name, which is the same as the image name here
-p: followed by the port mapping between the host and the container, before ":" is the server port, after ":" is the
last port of the seata container is the image name

You can see that the container is running.

insert image description here

2. Use docker-compose to deploy

Using docker-compose to deploy services is based on docker deployment, adding docker-compose.yml files to manage service containers in a unified manner, and implementing a command to start or close all service containers, so to use docker-compose deployment, you must first learn to use docker deployment , is a comfortable deployment method that is troublesome in the early stage and comfortable in the later stage.

Ruoyi also has related tutorials:

http://doc.ruoyi.vip/ruoyi-cloud/cloud/dokcer.html#docker-compose

1. Prepare the jar package and dockerfile for the service

On the basis of keeping the jar package and dockerfile of the gateway in Ruoyi, add the jar package and dockerfile of auth. The dockerfile of auth is similar to the gateway, as follows:

# 基础镜像
FROM  openjdk:8-jre
# author
LABEL author="gan"

# 创建目录
RUN mkdir -p /home/jar
# 指定路径
WORKDIR /home/jar
# 挂载目录
VOLUME /home/jar

# 复制jar文件到路径,第一个是宿主机文件目录(dockerfile所在目录,不能是绝对路径),第二个是容器文件目录
COPY ./ruoyi-auth.jar /home/jar/ruoyi-auth.jar
# 启动系统服务
ENTRYPOINT ["java","-jar","-Xms256M","-Xmx256M","ruoyi-auth.jar"]

insert image description here

Upload to specified folder

insert image description here

insert image description here

2. Write the docker-compose.yml file

The docker version corresponding to the docker-compose.yml file on the official website
insert image description here

Here is my docker version:

insert image description here

So the file version of my docker-compose.yml is 3.8.

There are only gateway and auth-related contents in the file, as follows:

# 描述 Compose 文件的版本信息
version : '3.8'

# 定义服务,可以多个,每个服务中定义了创建容器时所需的镜像、参数、依赖等
services:
  ruoyi-gateway:  # 服务名称,跟构建镜像名称有关,假如image值为空,那么生成的镜像名称为“docker-compose.yml文件所在目录名称_ruoyi-gateway”,我放在jar目录,生成的镜像名称就是jar_ruoyi-gateway
    image: ruoyi-gateway  # 构建镜像名称
    container_name: ruoyi-gateway   # 容器名称
    build:
      context: ./gateway    # 构建镜像的jar包和dockerfile文件所在的目录(相对于docker-compose.yml文件,我这里gateway目录是跟docker-compose.yml文件同一目录)
      dockerfile: dockerfile  # 构建网关镜像的文件名称
    ports:
      - "8010:8010"   # 构建容器端口号,“:”前为宿主机端口,“:”后为容器端口
  ruoyi-auth:
    image: ruoyi-auth
    container_name: ruoyi-auth
    build:
      context: ./auth
      dockerfile: dockerfile
    ports:
      - "9200:9200"

After the file is written, upload it to the specified directory

insert image description here

3. Common commands for docker-compose

(1) Create a mirror in the foreground and start the container

Similar to using the java -jar command to start the jar package, pay attention to execute it in the directory where the docker-compose.yml file is located, the command is as follows:

docker-compose up

insert image description here
Once the window is closed, the service will stop. It can be used when starting the service for the first time. The service name is on the far left. If each service does not see the log error, it means that the startup is successful.

insert image description here
Ctrl+C to exit, but even if it is closed, the created image and container still exist

insert image description here

(2) Create a mirror in the background and start the container

It should also be executed in the directory where the docker-compose.yml file is located. The command is as follows:

docker-compose up -d

insert image description here

You can also specify the path of the docker-compose.yml file, the command is as follows:

docker-compose -f /www/wwwroot/changjing/jar/docker-compose.yml up -d

insert image description here

The execution will not print the log of each service startup, you need to view the log, you can use the following command to view it

docker-compose logs

insert image description here
If you only want to see the log of the ruoyi-auth service, you can use the following command

docker-compose logs ruoyi-auth

insert image description here

(3), view the started container

You can view all containers started by the docker-compose.yml file, the command is as follows:

docker-compose ps

insert image description here

You can also specify the service name, for example, execute to see if ruoyi-auth is started, the command is as follows:

docker-compose ps ruoyi-auth

insert image description here

Note that it can only be executed in the directory where the docker-compose.yml file is located, and an error will be reported when executed in other directories

insert image description here

4. Stop service (including deletion)

To stop and delete all service containers without deleting the image, the command is as follows:

docker-compose down

You can see that the image is still there, but the corresponding container has been deleted.

insert image description here
To view the image corresponding to the service container, the command is as follows:

docker-compose images

insert image description here

Stop and delete the containers, networks, and mirrors of all services in the project. The command is as follows:

docker-compose down --rmi all

insert image description here
This method can be used to stop if the jar package is re-uploaded.

5. Start the service container

Start the containers of all services in the project, the command is as follows:

docker-compose start

insert image description here
You can also add the container name to the above command to start the specified container. For example, I only start the gateway container. The command is as follows:

docker-compose start ruoyi-gateway

insert image description here

6. Close the service container

Close all service containers in the project, the command is as follows:

docker-compose stop

insert image description here
You can also add the container name to the above command to close the specified container. For example, I only close the auth container. The command is as follows:

docker-compose stop ruoyi-auth

insert image description here

7. Restart the service container

Restart all service containers in the project, the command is as follows:

docker-compose restart

insert image description here

You can also add the container name to the above command to restart the specified container. For example, I only restart the auth container. The command is as follows:

docker-compose restart ruoyi-auth

insert image description here

Guess you like

Origin blog.csdn.net/studio_1/article/details/132235696