Backend——"Deploy springboot project to docker on windows and linux

Contents of this article 

  1. Install docker under win10 (non-key): 
  2.  Common commands of docker
  3. Deploy the project on docker and deal with the pits encountered

1: Install docker under win10

Docker allows installation on win10 professional version, but many computers are pre-installed win10 home version, this needs to be cracked and installed, there are many online tutorials, if there is a problem with the installation, here is a blog (invasion) https:// blog.csdn.net/li_chunlong/article/details/105531124 . Here are two important concepts of docker: container and mirror. The image runs in a container, just like a jar package runs on a virtual machine. The image can be compared to a jar package and the container is analogous to a virtual machine.

2: The most common commands of docker

  • 1、docker version
  • (Use after installation) Check the version number. After the installation is complete, you can check the version with this command.
  • 2、docker info
  • (Use after installation) View some configuration information of the container. (If you use the docker info command to output error during connect: Get http://%2F%....Barabara. Then congratulations on stepping on the pit, reinstall it).
  • 3、docker images
  • (Can be used before and after project deployment) Check which mirrors are available locally. You can follow the parameters -a, eg: docker images -a to view all images
  • 4、docker rmi 8ae296c2e568
  • (Delete image) Delete the image according to the IMAGE ID. The IMAGE ID can be viewed through the docker images command. In this example, 8ae296c2e568 is the IMAGE ID. Of course, it can also be deleted based on the image name and label.
  • 5、docker ps
  •   (It can be used before and after project deployment) Check which containers are in use. You can follow the parameters -a, eg: docker ps -a to view all containers
  • 6、docker stop 622a496650c9
  •  (Stop the container) Stop the container according to the CONTAINER ID, which can also be understood as stopping the project deployed in this container. After stopping, the port number occupied by the container is released
  • 7、docker rm   622a496650c9
  • (Delete container) Delete the container based on the CONTAINER ID.
  • 8、docker run -p 8083:8082 main-system
  • (Starting the mirror) docker run is the starting mirror. The meaning of -p 8083:8082 is to expose the port 8082 mapping of the project in the form of port 8083 for others to call (somewhat similar to nginx). The port 8083 on the left is customized. Port 8082 on the right is the port number of the java program or tomcat. main-system is the name of the image. The whole command means to select the main-system image from the mirror repository, and expose the program port 8082 loaded by this image to the caller as the port number 8083. After using this command, the program with port number 8082 is isolated in the container, and the entrance and exit are exposed with port number 8083, and will not occupy the current port number of the computer 8082, which means that a port number 8082 can also be started on the computer program of.
  • 9、mvn package -P test docker:build
  • (Compile the image) Package the project into a jar package through the mvn package command, and put it on docker to compile

 Personal understanding of mirrors and containers: First, use the mvn command to type the springboot project into a jar package, and then put the jar package on docker to compile, that is, the 9th command above. After the compilation is completed, a mirror is formed, which can be done through docker images see. Then start the image through the docker run command, which is equivalent to putting the image in the container for startup. After startup, you can view the running container through docker ps.

3: Deploy the project on docker and deal with the noteworthy points encountered

To let the springboot project run on docker, life insurance needs to add the docker plug-in to the pom.xml file, and then create a new Dockerfile file

   pom.xml :

<build>
        <plugins>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                   <imageName>main-system</imageName><!--镜像名称,可自定义-->
                   <dockerDirectory>src/main/docker</dockerDirectory><!--Dockerfile文件路径,可自定义-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>

  Dockerfile :

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD main-system-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  • The first point: No  such  file  or  directory error is reported after executing the docker run command . This error is mostly because the path specified in the dockerDirectory of the docker plug-in in the pom.xml file is not in the same path as the Dockerfile file, so just change it to the same path.
  • The second point: The driver failed programming external connectivity on endpoint affectionate_greider error is reported after the startup command is executed. This is because there are already running containers that occupy the port in the startup command. You need to use the docker stop containerid command to make it running Container stopped
  • The third point: After using the docekr run command to successfully start the project, an error Unable to connect to 127.0.0.1:6379 appears when using postman to call the interface in the project. This is because of the redis configuration file. First, find the file redis.windows.conf in the redis directory, comment out the bind 127.0.0.1 in the file, and change the protected-mode yes to protected-mode no at the same time, and restart Redis service; then change the 127.0.0.1 in spring.redis.host=127.0.0.1 to the local intranet ip in the springboot configuration file application.properties, compile the new image and then run it.
  • The fourth point: startup error ehcache.xml (No such file or directory). This problem has been seen for a long time. It is okay to run through mvn ordinary packaging, but it starts to report this error when it is run in docker. Finally, it is found that there is a problem with the file path of the cache configuration file ehcache.xml. Need to put this file under src/main/resources, and then read this file path in shiro configuration, you need to use this.getClass().getClassLoader().getResource("ehcache.xml").getPath() To get it. Finally, comment out the maxEntriesLocalHeap and maxElementsOnDisk attributes in the ehcache.xml file.

The above is the deployment of docker on windows. But most of the current programs are developed on windows and deployed on linux. Then the deployment process of ordinary people is

Development on windows-"compile mirror on windows-"submit mirror on windows-" pull mirror on linux-"run mirror on linux.

Compilation on windows has been mentioned in the previous article, then this process involves another important concept of docker, namely: warehouse. The warehouse is well understood. It is the place to store the mirror image, similar to the git warehouse for code branches and the maven warehouse for dependent packages. Just like the git repository has many hosting platforms such as github, coding, and code cloud, the docker mirror repository also has dockerhub, Alibaba Cloud mirror repository, and NetEase mirror repository. In the following steps, we take Alibaba Cloud Mirror Warehouse as an example

  • The first step is to register and log in to the official website of Alibaba Cloud, then search for "Container Image Service" and click "Management Console".
  • Step 2: Click Create Mirror Warehouse, fill in the custom namespace, warehouse name, and summary. Click Next to select a local warehouse. After creation, it is as follows.
  • Step 3: Submit the image on windows: first execute docker images, you can view the compiled image in the local warehouse. (The address of push in the figure below is registry.cn-shanghai.aliyuncs.com/manage-system/staging:1.0.0, where manage-system is the namespace, staging is the warehouse name, and 1.0.0 is the version number. This can be from Definition), after the upload is complete, open the image version of the container image service to see the image just uploaded.
    D:\Work\Project\baoji-system\webapps\manage-system>docker login --username=yourname registry.cn-shanghai.aliyuncs.com
    Password:
    Login Succeeded
    
    D:\Work\Project\baoji-system\webapps\manage-system>docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    manage-system       latest              291da7fabc50        About an hour ago   183MB
    openjdk             8-jdk-alpine        a3562aa0b991        15 months ago       105MB
    
    D:\Work\Project\baoji-system\webapps\manage-system>docker tag a3562aa0b991 registry.cn-shanghai.aliyuncs.com/manage-system/staging:1.0.0
    
    D:\Work\Project\baoji-system\webapps\manage-system>docker push registry.cn-shanghai.aliyuncs.com/manage-system/staging:1.0.0
    The push refers to repository [registry.cn-shanghai.aliyuncs.com/manage-system/staging]
    ceaf9e1ebef5: Pushed
    9b9b7f3d56a0: Pushed
    f1b5933fe4b5: Pushed
    1.0.0: digest: sha256:44b3cea369c947527e266275cee85c71a81f20fc5076f6ebb5a13f19015dce71 size: 947
    

  • Step 4: Install the mirror on linux, here is a blog recommended (invasion) https://blog.csdn.net/u014069688/article/details/100532774

  • Step 5: Pull the mirror image. Log in first and enter docker pull registry.cn-shanghai.aliyuncs.com/nameSpace/depository:version-num. Like the above, nameSpace is the namespace, depository is the warehouse name, and version-num is the version number. Replaced here with our own. After the replacement is completed, enter the pull command. If an error occurs: Error response from daemon: pull access denied for xxx.xxx, repository does not exist or may require'docker login', please pay attention here. If this problem occurs in the pull, we need Configure an Alibaba Cloud accelerator (step 6), if there is no problem with the pull, skip step 6

  • Step 6: Configure Alibaba Cloud Accelerator

    1:到docker的安装目录中去
    cd /etc/docker/
    2:编辑或创建docker配置文件daemon.json
    vi daemon.json
    输入i插入字符,将以下json配置粘贴进去
    {
      "registry-mirrors": ["https://brf9xytm.mirror.aliyuncs.com"]
    }
    
    按esc退出编辑模式,输入英文冒号: ,再输入wq保存文件
    3:重启docker服务
    systemctl daemon-reload
    systemctl restart docker

    After the configuration is restarted, log in and pull again and the pull will be successful, as follows

  • Step 7: Run mirroring, this step has already been discussed in the article. Let's enter the following command to run it successfully. Among them, the -p parameter has been mentioned, and the -d parameter means that we will run the program in the background. After input, it will return a string of container id, which does not affect our input of other commands.

 So far, the three key points of docker warehouse, mirror, container, as well as the pits encountered during installation and use, and the deployment process have been written almost. But another technical point closely related to docker, kubernetes (k8s cluster deployment ) , will have the energy to update it later.

If there are any errors in the text, please correct me...

Guess you like

Origin blog.csdn.net/nienianzhi1744/article/details/107953467