Article Directory
Deployment steps
1.1, project packaging
Type the SpringBoot project into a jar package.
1.2, upload to the server
Use the file upload tool to upload the jar package to the server. The following upload tool is WinSCP
You can write a Dockerfile in Windows, and then upload it directly to the server. It can be used directly at that time.
1.3, the preparation of the Dockerfile file
# FROM表示使用的基础镜像为JDK8
FROM java:8
# 设置镜像时区
RUN echo "Asia/Shanghai" > /etc/timezone
# 添加当前路径下的blog-backend-1.0-SNAPSHOT.jar到镜像中,并改名为app.jar
ADD /blog-backend-1.0-SNAPSHOT.jar app.jar
# 镜像入口,镜像启动时运行
ENTRYPOINT ["java","-jar","/app.jar"]
# 暴露的端口
EXPOSE 8091
1.4, log in to the server
Use the tool putty to log in to the server.
Enter the path where the Dockerfile and jar package are located
1.5, build a mirror
Use docker build -d blog-backend .
build image.
docker build :Docker构建镜像命令
-d blog-backend :表示给镜像取名为blog-backend
. :表示在当前路径构建镜像
1.6, boot image
Use the docker run -d --net host blog-backend
background image to run.
You can also use the function to docker run -it --net host blog-backend
run the mirror, and related logs will be printed.
docker run :容器启动命令。容器可以看做另一个电脑,虚拟机。
-d :后台运行
-i : 以交互模式运行
-t : 为容器再分配一个伪输入终端
-p : 可以指定要映射的IP和端口。比如-p 9000:8080,表示宿主机端口9000跟容器内端口8080映射。
--net host : 设置容器的网络直接与宿主机相连。比如需要连接mysql为localhost:3306,
项目暴露地址为8091。如果不使用--net host参数,容器会使用自己内部的网络,
连接容器自己的3306端口,暴露自己的8091端口。但是外网无法访问,因为外网
访问的是宿主机。所以可以使用-p参数端口映射,将容器的端口映射到宿主机,
但也无法连接上mysql,所以直接使用--net host,让容器使用宿主机的网络,就
可以直接连接宿主机的mysql,直接将容器项目的8091映射到宿主机。