Docker deployment SpringBoot+Mongo project example (personal record)

Docker integrates SpringBoot and MongoDB

1. Install Docker

Currently, DockerHub only supports Win10 Professional Edition, Enterprise Edition, and Education Edition to directly install Docekr Desktop for Windows. Home Edition, Win7 and Win8 cannot be installed for the time being, and only Docker ToolBox can be installed. The following is the installation tutorial of Docker ToolBox

Go to http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ to choose the version to download. Note that it is best to turn off the Hyper-V function before installation. (as opposed to installing Docekr Desktop for Windows)

insert image description here

If there is no Hyper-V in the function, you can ignore it. After the installation is successful, there will be three more icons on the desktop

insert image description here

2. Pull the MongoDB image

Open the Docker Quickstart Terminal (the little whale icon) and run docker search mongo to find mongo images that can be pulled. We pull the latest mongo image, enter docker pull mongo:latest, after the download is complete, we check whether the image already exists locally and enter docker images

insert image description here

Of course, we can also open Kitematic (the icon looks like a blue K), which is a graphical docker interface. Log in to the docker account, enter the interface, you can directly click on the image you want to pull, or search for the image we want

insert image description here

For example, we want to pull the redis image, click CREATE, and you will find that it is automatically downloaded. But the speed may be slower

insert image description here

If you are not used to Docker Quickstart Terminal, you can choose PowerShell

insert image description here

Put the image into the container to start

We can enter dockerhub to find the corresponding command. For example, I want to put the mongo image into the container and start https://hub.docker.com/_/mongo

First enter dokcer run --name mongo -d mongo:latest to put the mongo image into the container and start it. After success, enter docker ps to view the container being started

insert image description here

Enter the mongo container and create a new mongo database, and user

Enter docker exec -it mongo bash and then (root@/) will appear (root@/) like linux, enter mongo to enter the container, and the mongo version will appear to indicate success. As shown below:

insert image description here

Create a new database user by entering use tableName (your database name), and then enter db.createUser({user: "The username you want", pwd: "The password you set", roles: [{ role: "dbOwner", db: "your database name" }]}). After the creation is successful, you can verify whether it has been successfully created through db.auth().

insert image description here

Of course, if you think the command line is too troublesome, you can also use graphical operations. Here you can set the port, rename the container, delete and other functions

insert image description here

3. Build a SpringBoot project to integrate Docker and MongoDB

Create a SpringBoot project and import it in maven

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.8.2</version>
    </dependency>

       <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.0.0</version>
            <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>

Configure application.properties

Pay attention to the red part, because the format of the new user we added in docker mongoDB is: user='', pwd='', so we also need to configure it like this here.

Write a simple example project structure as follows

insert image description here

The example is as follows: We can call the MongoDB packaged by SpringData to directly call the methods in the MongoTemplate to realize simple addition, deletion, modification and query.

Create entity class

insert image description here

Call MongoTemplate method

insert image description here

Start the project and test it with postman

Add user function

insert image description here

Find all user functions

insert image description here

4. Write Dockerfile to let the project run on docker

To connect to docker, the default built-in default is used here. After the connection is successful, it will display the existing images and running containers of docker

insert image description here

Go to Edit Configurations

insert image description here

Configure the Dockerfil location, and add the maven command: clean package -Dmaven.test.skip=true

insert image description here

insert image description here

Write Dockerfile

FROM openjdk:8
VOLUME /tmp
ADD target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

insert image description here

After running the Dockerfile successfully, it will appear in the container, and the SpringBoot project startup success message will appear in the log

insert image description here

The postman test finds the user successfully

insert image description here

5 Stepping on the Pit Collection

DockerTool Box stepping pit collection

1.DockerTool Box startup report (default) No default Boot2Docker ISO found locally, downloading the latest release… error

Need to download the latest boot2docker, you can download it at https://github.com/boot2docker/boot2docker/releases , replace the boot2docker.iso in C:\Users\username.docker\machine\cache

2.DockerTool Box starts and reports VBoxManage.exe: error: Raw-mode is unavailable courtesy of Hyper-V. (VERR_SUPDRV_NO_RAW_MODE_HYPER_V_ROOT) error

Start cmd in administrator mode and enter bcdedit. Make sure that the part in the red circle in the figure below is off. If it is Auto, enter bcdedit /set hypervisorlaunchtype off and restart the computer.

insert image description here

Pit of Dockerfile ADD

Dockerfile ADD needs to specify the path, if not specified, there will be docker-maven-plugin ADD failed: no source files were specified error

ADD does not specify a path

insert image description here

ADD specifies the path

insert image description here

Pit to connect to Docker

If you want to connect to docker on your own server, you need to add an inbound rule with port 2375 on the server, otherwise the idea will fail to connect

ADD specifies the path

Pit to connect to Docker

If you want to connect to docker on your own server, you need to add an inbound rule with port 2375 on the server, otherwise the idea will fail to connect

Guess you like

Origin blog.csdn.net/eddiead/article/details/125897193