Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451
Overview
This article will introduce the installation and basic use of Docker, and how to deploy SpringBoot projects in Docker.
1. Why use Docker
Developer Xiao Ming saw the bug submitted by the test and said: The program runs well on my machine, how come you are not working?
When the test king saw the problem report sent by Xiao Liu from the operation and maintenance, he said: The program is well tested here, how can it not work on the customer side?
This is actually the daily routine of software development. One of the most troublesome problems in software development is environment configuration.
A project needs the support of a large number of programs to run, including: jdk, tomcat, mysql, redis, nginx, etc.... The environment configuration of each program may be different, which can easily lead to: it can run on your machine, He can't run on his machine.
How to solve this problem?
1.1 Containerization
The answer is: containerization
Containerization is to package the program and put it in a container, and then copy the container when needed, so that the program environment configuration in the container can be kept consistent, which fundamentally solves the problem of inconsistent environment configuration.
1.2 Virtual machines and containers
When thinking of containerization, some students may think of the virtual machine we used before. After the virtual machine is cloned, the software environment configuration inside is also the same.
Virtual machine and container architecture
You can see that the biggest difference between a virtual machine and a container is:
There is an additional operating system in the virtual machine, and the container and the host share the operating system, which is more lightweight.
Comparison of containers and virtual machines
characteristic | container | virtual machine |
---|---|---|
Start Time | Second level | Minute level |
Hard disk usage | Generally MB | Generally GB |
performance | Close to native | Weaker than native |
System support | Single machine supports thousands of containers | Usually dozens |
Containers have advantages over virtual machines in terms of resource occupation and performance, and virtual machines are stronger than containers in terms of system functions and security.
2. Introduction to Docker
2.1 What is Docker
Docker is an open source application container engine that allows developers to package their applications and dependent packages into a portable container, and then publish it to any popular Linux machine. It can also be virtualized. The container is completely sandboxed. Mechanism, there will be no interfaces between each other.
Docker is very similar to the operating system of containers. Containers are containers. Inside are the programs we need. These containers are isolated from each other. When they are needed, they are transported from freighters, assembled by dock workers on cars and trains, and then sent to each Project locations.
2.2 Docker architecture
Important concepts:
- Host (server running Docker)
- Daemon daemon (execute Docker operations in the background)
- Registry Warehouse service registration (remote server, save a large number of warehouses)
- Repository (the location where the image file is saved)
- Image (Save process or software file)
- Container (process after the image is running)
Docker adopts C/S mode (client/server mode), the main operations are build (build), pull (pull), run (run), if users need to use mysql, they will connect to the host through the client, and the guard in the host The process will pull mysql from the remote service registration, save it in the image file in the host, and then build the mysql image. The completed image can create a mysql container, and the running mysql container can provide services to users.
3. Install Docker
1. Delete the old version of docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
2. Install dependent packages
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
3. Install docker
yum install docker
4. Start docker
systemctl enable docker
systemctl start docker
5. View the installation results
docker version
6. Configure mirroring
Speed up the image download, create or modify the /etc/docker/daemon.json file, and modify it to the following form
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
7. Restart
systemctl restart docker
4. Basic use of Docker
Common Docker commands
docker images 查看镜像
docker search 镜像名称 搜索镜像
docker ps 查看容器
docker stop 容器id 停止容器
docker rm 容器id 删除容器
docker rmi 镜像id 删除镜像
docker run 镜像id 运行镜像
docker pull 镜像名 拉取镜像
docker build 镜像名 . 创建镜像
1) Grab the warehouse
docker pull library/hello-world
2) View the mirror
docker images
4) Run the mirror
docker run hello-world
5. Docker install MySQL
1) Install MySQL
docker pull mysql:5.7
2) Run MySQL mirror
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql镜像ID
The port behind -p is the local port, which is mapped to port 3306 of MySQL in docker, and the root password is set later
-d is running in the background
3) View the container ID
docker ps
4) Upload the database script to Linux, and then copy the script to the root directory of docker
docker cp bookdb.sql 21db7dd6618e:\root
5) Enter the docker container
docker exec -it 21db7dd6618e bash
6) Enter MySQL
mysql -uroot -p123456
7) Set coding, create database
set names utf8;
create database bookdb;
use bookdb;
8) Import sql script
source /root/bookdb.sql;
9) Modify remote access permissions:
select host,user,plugin,authentication_string from mysql.user;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
- Exit mysql and docker
exit
6. Deploy the Springboot project on Docker
1) Modify the pom file
First add the Docker image name in the pom.xml file of the springboot project:
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
Add the docker build plugin in the plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker maven plugin -->
<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>
<!-- Docker maven plugin -->
</plugins>
</build>
2) Create a Dockerfile
FROM java:8
VOLUME /tmp
COPY jar文件名 app.jar
RUN bash -c "touch /app.jar"
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=test", "--server.port=8080", "> /log/app.log"]
Content explanation:
FROM java:8是获取到远程仓库的jdk1.8镜像。
VOLUME指向了一个/tmp的目录,由于Spring Boot使用内置的Tomcat容器,Tomcat默认使用/tmp作为工作目录。效果就是在主机的/var/lib/docker目录下创建了一个临时文件,并连接到容器的/tmp。
将项目的jar文件复制为app.jar添加到容器
RUN表示在新创建的镜像中执行一些命令,然后把执行的结果提交到当前镜像。这里使用touch命令来改变文件的修改时间,Docker创建的所有容器文件默认状态都是“未修改”。这对于简单应用来说不需要,不过对于一些静态内容(比如:index.html)的文件就需要一个“修改时间”。
EXPOSE 容器暴露端口
ENTRYPOINT 应用启动命令 参数设定
3) Create a docker folder, move the jar package and Dockerfile to this folder, enter the folder, and run the command:
docker build -t 镜像名称 .
4) View the mirror
docker images
5) Run the mirror
docker run --rm -p 8080:8080 --link mysql容器ID:localhost docker镜像名称或ID
--Rm means the container will be deleted automatically
-p means port mapping, the left side of the colon is the port number outside the docker container, and the right side is the port number inside the container
-d means running in the background
--Link means to connect to the mysql container (if the project does not require a database, you can remove it), the left side of the colon is the id of the mysql container followed by the alias of the container, so that the localhost configured in jdbcUrl will be converted to the docker container of mysql
6) Solve the problem that Docker cannot connect to the Internet
If the following prompt appears after starting the project, it means that there is a problem with the Docker network
Solution
vi /etc/sysctl.conf
Add ipv4 forwarding
net.ipv4.ip_forward=1
Restart network
service network restart
Test through the browser after restarting the Docker image
7) Save the image to the remote warehouse
Log in to Docker hub, enter your account and password
docker login
Push the image to docker hub
docker push 镜像名称
End
This article takes you through the introduction of Docker and deploying a simple project. If you find it useful, please give it a thumbs up.
If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary