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
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
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
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
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
If you are not used to Docker Quickstart Terminal, you can choose PowerShell
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
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:
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().
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
3. Build a SpringBoot project to integrate Docker and MongoDB
Create a SpringBoot project and import it in maven
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
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
Call MongoTemplate method
Start the project and test it with postman
Add user function
Find all user functions
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
Go to Edit Configurations
Configure the Dockerfil location, and add the maven command: clean package -Dmaven.test.skip=true
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
After running the Dockerfile successfully, it will appear in the container, and the SpringBoot project startup success message will appear in the log
The postman test finds the user successfully
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
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.
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
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
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