Use docker to deploy springboot microservice projects

1. Environment preparation

Check if docker is installed

[root@localhost /]# docker -v
Docker version 1.13.1, build 7d71120/1.13.1

[root@localhost /]# systemctl status docker #查看docker Engine 的状态
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 六 2023-07-29 22:49:00 CST; 7h ago
     Docs: http://docs.docker.com
 Main PID: 967 (dockerd-current)
    Tasks: 155
   Memory: 285.3M
   CGroup: /system.slice/docker.service
............

1. Prepare the jar package project used

Create a docker directory under the /opt/ directory:
Upload the jar package of the executable project packaged by Maven in Idea to the /opt/docker directory of the server. The
insert image description here
service port in the yml configuration file in the project is 8096:
insert image description here

2. Write the corresponding Dockerfile

The file name used to describe the build project image must be Dockerfile, without suffix, and the file name cannot be arbitrarily named. Otherwise, it will prompt that the Dockerfile cannot be found when building.

# 基础镜像,也叫父镜像,因为jar的运行依赖于JDK或Jre,这里使用只拥有运行环境的Jre,减少最终生成的镜像的大小。
FROM openjdk:8u212-jre
# 维护者
MAINTAINER ssccxx
# 在宿主机的 /var/lib/docker目录下的volumes目录里会创建一个临时文件随机名称的目录将这个目录下的_data 链接到容器内的 /tmp
VOLUME /tmp  #会自动在容器内的根目录下生成此tmp目录
# 将项目的jar添加到镜像中,并修改名称为myapp.jar,这里默认添加到容器的根目录下(即默认的工作目录)。
ADD platform-customer-post.jar myapp.jar
# 在构建镜像时会执行RUN 命令 ,这里的 touch /myapp.jar 不起作用,因为上方的ADD里已经使根目录下有同名的文件。这里的touch就不会再生效了。
RUN bash -c 'touch /myapp.jar'
# ENTRYPOINT 是 在容器运行时自动执行的命令,也就是启动容器内的项目服务
ENTRYPOINT ["java","-jar","/myapp.jar"]
# 声明, 此容器内的服务监听端口应该为8096,这里的EXPOSE只是声明,方便之后的维护者一目了然可以知道容器内的服务的监听端口。
EXPOSE 8096

Note: FROM, ADD, and RUN in the instructions in the above Dockerfile are all executed when the image is built. And ENTRYPOINT, VOLUME, EXPOSE are
executed when the container is running.

3. Build a mirror image

[root@localhost docker]# ll
总用量 79156
-rw-r--r-- 1 root root      178 7月  30 01:36 Dockerfile
-rw-r--r-- 1 root root 81049868 7月  29 23:02 platform-customer-post.jar
[root@localhost docker]# docker build --no-cache -t micro-server:v1.0 .

说明:build is the build command -t, you need to specify repository:tag (that is, warehouse name:tag tag, because these two can determine a mirror image).
repository name must be lowercase: If the custom warehouse name has uppercase letters, this will be prompted. Change it all to lowercase.
--no-cache: Indicates that the image cache that existed in the previous build will not be used when building. Generally, this is added, otherwise it is possible to use the built image in the docker engine cache, resulting in
duplicate image IDs and conflicts.
If there is no named Dockerfile in the current /opt/docker directory. It will prompt that the Dockerfile file is missing.
重点The last . indicates the context path: here is the current location where our docker build command is executed (opt/docker directory).
Because the running mode of docker is C/S. Our local machine is C (client), and the docker engine is S (server). The docker may be on one machine, and the docker engine, that is,
the corresponding server may be on another machine. We send the build information to the docker engine through the CLI command of the docker client. The actual build process is
completed under the docker engine, so we cannot use our local files at this time (for example, we need to use the platform when building here. -customer-post.jar file).
This requires the files in the specified directory of our local machine to be packaged and provided to the docker engine for use. Although the virtual machine demonstration environment we are using now is that the C/S are all on the same machine
. But you have to go through this process. The content in the /opt/docker/ directory will be packaged and sent to the engine for use, so that the docker engine needs to copy when executing the ADD command
platform-customer-post.jar to the image, you can find it from the packaged content. After all the builds are completed and the final image is generated, the docker engine will automatically clean up the packaged and sent content
.
注意: Do not put useless files in the context path, because they will be packaged together and sent to the docker engine. If there are too many files, the process will be slow.

如果:构建时使用: docker build . --no-cache=true 命令 ,没有指定 生成的镜像的 repository:tag ,会产生悬虚镜像。		

insert image description here
Suspended mirror image: It is <none>the mirror image of both repository and tag.
Such a mirror image has no practical meaning. can be deleted to free up disk space

[root@localhost docker]# docker images -f dangling=true #查询出全部的本地悬虚镜像
[root@localhost docker]# docker image prune  #删除所有悬虚镜像
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y #键入y表示删除所有悬虚镜

insert image description here
Use docker images to see if the built image exists:
insert image description here

4. Run the image

Generate container instances from images

  1. docker create --name container instance name -p host port: the port image id of the service in the container
    说明: The docke create command is to create a container with the image corresponding to the specified image id, but it does not directly run the container instance. By docker ps -aviewing all containers (including running running
    stopped container), and then use it with docker start container id or container name.
  2. docker run -d --name container instance name -p host port: the port of the service in the container image id
    说明: The docker run command will create a container based on the image corresponding to the specified image id, and run the generated container instance directly. -d means to run the container in the background, otherwise it will
    print the content on the console.
    说明: Do not specify –name container name, the system will automatically generate a container name: name with adjective plus underscore and name. Note the placement of the --name attribute.
    说明: docker start command. Can only be used for existing containers, not for creating and starting containers.
    推荐 使用 docker run 这个命令. Because of the problem of checking whether the host port is already occupied when using docker create, it is only checked at runtime.
[root@localhost _data]# docker run -d --name xiaohai -p 8090:8096 79bcb7a73dd2
fdb21e089157e0080ecf26585f5ac174f3e56c056f4d341d838387d048debc5e #执行成功后,会返回容器实例id的完整部分。
[root@localhost _data]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
fdb21e089157        79bcb7a73dd2        "java -jar /myapp.jar"   2 seconds ago        Up 2 seconds        0.0.0.0:8090->8096/tcp   xiaohai

说明:
CONTAINER ID: container instance id
IMAGE: mirror id
COMMAND: the command executed automatically inside the container when the container starts
CREATED: the past time from when the container was created to the current time
PORTS: indicates the mapping and protocol from the host to the application port in the container
NAMES: container instance name

According to the same image, we can create multiple container instances on the same host. The container is directly isolated from the container, and the same service port will not conflict. Just make sure that the ports on the host are inconsistent. Externally, by requesting the host ip:host port/application service interface, it will be forwarded to the service in the corresponding container according to the port mapping relationship.
insert image description here

5. Test whether the service is OK

[root@localhost _data]# curl http://localhost:8090/v3/api/client/v1/captchaImage

Check the container log:
docker logs -f container id,
insert image description here
you can see that the request has come in, indicating that the service in the container is normal.
说明:You can view the corresponding logs through docker logs -f container id if the container fails to start or whether the service is normal.

6. Port description

1. Host port, EXPOSE port, and service port in the application service configuration file. the relationship between these three.
Let me talk about the EXPOSE port first, which is declared in the Dockerfile configuration file. In fact, this is for those who maintain this configuration file later. When the EXPOSE port is executed,
the port will not actually be monitored. It just means that the port that our application service listens to is this. The specific container application service port is the port when the service in the container is started (that is, the service port in the application service configuration file). Not writing the EXPOSE port in the Dockerfile has no effect at all. It's just inconvenient for other personnel to maintain later.
In general, the EXPOSE port should be consistent with the service port in the application service configuration file, so that other personnel can know the port number of the future application service in the container by reading the Dockerfile
.
2. Description of port mapping. -p host port: container application service port (service port in the application service configuration file).
What will happen if this port mapping is not configured. The impact is: there are only two ways to access the services in the container. 1. On the host machine,
access through the container ip: the port of the application service in the container. 2. Enter the container and access it through the container ip or localhost: the port of the application service in the container.
What would be the benefit if this port mapping is configured. The advantage is that it can be accessed on the host through localhost or host ip:host port. It is also possible to
indirectly access the application services in the container by accessing the host ip:host port on the external machine, which is convenient for the external network to access the services in the container.
说明: The host machine mentioned here refers to the node machine running the container. This node machine is a virtual machine or other physical machine installed through VMware.
My demonstration environment here is that the physical machine is a Windows system machine with VMware Station installed on it, and a virtual machine installed through VMware Station. The container runs on this virtual
machine. The container host mentioned above is this virtual machine. Not my physical machine.

7. Enter the container

[root@localhost _data]# docker exec -it fdb21e089157 /bin/bash  #-it后 是容器实例id  /bin/bash指的是进入容器后,打开一个bash终端  #构建镜像时没有指定工作目录,则默认进入容器后的目录为容器内的根目录
root@fdb21e089157:/# ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  myapp.jar  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#可以看到myapp.jar包就在容器的根目录下,包括挂载点 tmp目录
#查看容器的java环境,可以看到java版本已经 java的安装目录
root@fdb21e089157:/# java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

root@fdb21e089157:/# which java
/usr/local/openjdk-8/bin/java
#也就是我们这个容器内已经有个可执行文件jar包的运行环境了。
root@fdb21e089157:/usr/local/openjdk-8/bin# pwd
/usr/local/openjdk-8/bin
root@fdb21e089157:/usr/local/openjdk-8/bin# ls
clhsdb	hsdb  java  jjs  keytool  orbd	pack200  policytool  rmid  rmiregistry	servertool  tnameserv  unpack200
#可以看到bin目录下只有java命令,没有 javac,jps等命令。因为我们用的基础镜像为openjdk:8u212-jre。所以么有java的编译等命令。只有运行的命令。而且java 命令也已经是容器内的环境变量了。在容器内的任意目录下都可以执行。所以在容器启动后,在容器内的根目录下 自动执行java -jar xxxx.jar 指令是没有问题的。所以宿主机不需要安装java环境。容器内已经拥有了自己的java环境了。
#容器就像一个精简版的centos或ubuntu,里面只有极少的命令如ls,df,du , 没有ll,vim,netstat,yum等命令。
root@fdb21e089157:/# du -h myapp.jar #可以用这个du -h 文件名 查看文件的大小
78M	myapp.jar

8. Common commands for operating containers

1. docker start 容器id  							#启动已停止的容器
2. docker stop 容器id  							#停止正在运行的容器
3. docker exec -it 容器id -- bin/bash        #进入容器内,注意只有当容器是运行时此命令才有效
4. docker ps 										    #查看正在运行的容器
5. docker ps  -a									    #查看本机所有的容器,包含已停止运行的容器
6. docker logs -f 容器id							#查看容器运行及内部服务查看的日志,若容器已被删除,这里的日志也就查不到了,会被跟着容器自动被删除。
7. docker  inspect 容器id							#查看容器内部组织结构信息,包括容器内的挂载点信息,容器的ip地址信息
8. docker rm 容器id                                  #删除已停止的指定的容器
9. docker rm -f 容器id								#删除容器(包括正在运行的容器)
10. docker rm -f $(docker ps -a -q)          # 删除所有容器,包括正在运行的容器,所有删除操作 因此,在执行该命令之前,请确保您的所有数据已经保存

Guess you like

Origin blog.csdn.net/adminstate/article/details/132000611