Spring boot is packaged into docker (idea + traditional method)
•
Java
1. Mode 1 Remote publishing via idea
1.1 modify docker Service file
vim /usr/lib/systemd/system/docker.service
2. Modify the following contents of execstart behavior and note out the original
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
3. Reload
systemctl daemon-reload // 1,加载docker守护线程
systemctl restart docker // 2,重启docker
4. Restart the original service with docker related commands
1.2 verification
1. Check the port status
netstat -nlpt
2. Call
curl http://127.0.0.1:2375/version
3. The following indicates success
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"20.10.6","Details":{"ApiVersion":"1.41","Arch":"amd64","BuildTime":"2021-04-09T22:43:57.000000000+00:00","Experimental":"false","GitCommit":"8728dd2","GoVersion":"go1.13.15","KernelVersion":"3.10.0-1127.19.1.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.4.4","Details":{"GitCommit":"05f951a3781f4f2c1911b05e61c160e9c30eaa8e"}},{"Name":"runc","Version":"1.0.0-rc93","Details":{"GitCommit":"12644e614e25b05da6fd08a38ffa0cfe1903fdec"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"ApiVersion":"1.41","Os":"linux","BuildTime":"2021-04-09T22:43:57.000000000+00:00"}
Tips
1. VIM search string
1. 命令模式下,输入:/字符串
比如搜索user,输入/user
2. 查看下一个匹配,按下n(小写n)
3. 跳转到上一个匹配,按下N(大写N)
4. 搜索后,我们打开别的文件,发现也被高亮了,怎么关闭高亮?
命令模式下,
输入:nohlsearch
也可以:set nohlsearch
当然,可以简写:
noh
或者 set noh。
2. Remember to turn on the safety team
3. For idea configuration, the name can be changed
Then it will appear in the service window
Click Run in the upper left corner to see the containers already on the server
4. Configure alicloud image accelerator. Baidu won't
1.3 edit code POM xml
<properties>
<java.version>1.8</java.version>
<!-- Step.1 设置docker镜像前缀-->
<docker.image.prefix>jinsc</docker.image.prefix>
</properties>
<!-- Step.2 添加插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!--远程docker地址-->
<dockerHost>http://x.x.x.x:2375</dockerHost>
<!--镜像名称,前缀/项目名-->
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--dockerFile的位置-->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<!--默认的target文件夹位置-->
<directory>${project.build.directory}</directory>
<!--最终名称.jar-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
1.4 create a new folder docker in main and a new dockerfile file
Dockerfile
FROM java:8
VOLUME /tmp
ADD provider-0.0.1-SNAPSHOT.jar /test.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/test.jar"]
Where provider-0.0 1-SNAPSHOT. Jar is the name of the typed jar package
1.5 run Maven command
Maven's clean + package
Run the build of docker in the plug-in
Can be pushed remotely to the far end
//镜像id
Successfully built 13b4c048a7e6
// 项目名和版本号
Successfully tagged jinsc/provider:latest
[INFO] Built jinsc/provider
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 52.797 s
[INFO] Finished at: 2021-10-22T15:07:58+08:00
[INFO] ------------------------------------------------------------------------
Connect to the server. It's already there
1.6 right click the docker in the idea service window to create a container
Click Run to create it
Just visit
This is the WS test address
The client written in has been connected and resolved to
2. Mode 2 Traditional mode
2.1 Maven package
2.2 put the package on the server
2.3 creating folders on the server
[root@VM-0-9-centos /]# cd usr/local/
[root@VM-0-9-centos local]# ls
bin etc games include lib lib64 libexec qcloud sbin share src yd.socket.server
[root@VM-0-9-centos local]# mkdir docker
[root@VM-0-9-centos local]# ls
bin docker etc games include lib lib64 libexec qcloud sbin share src yd.socket.server
[root@VM-0-9-centos local]# cd docker/
[root@VM-0-9-centos docker]# ls
[root@VM-0-9-centos docker]# mkdir SB4WS
[root@VM-0-9-centos docker]# ls
SB4WS
[root@VM-0-9-centos docker]#
2.4 put jar in
2.5 create and edit dockerfile
vim Dockerfile
FROM java:8
VOLUME /tmp
ADD provider-0.0.1-SNAPSHOT.jar /provider.jar
EXPOSE 8080
ENTRYPOINT ["java","/provider.jar"]
The meaning is
FROM :
表示基础镜像.运行环境
VOLUME /tmp:
是创建tmp目录并持久化到Docker数据文件夹,因为sb使用的是内嵌的Tomcat,默认使用/tmp作为工作目录
ADD:
拷贝文件并重命名,就是把前面的复制到后面的
EXPOSE :
并不是正真的发布端口,这个只是容器部署人员和建立image人员之间的交流,即简历image的人员告诉容器部署人员容器应该映射哪个端口给外界
ENTRYPOINT(入口点):
容器启动时运行的命令,相当于在命令行中输入 java -jar xxx.jar,为了缩短Tomcat 的启动时间,添加java.security.egd的系统属性指向 /dev/./urandom 作为ENTRYPOINT
2.6 create image (under the folder just now)
docker build -t provider .
Already OK
2.7 creating containers
docker run -d --rm --name provider-8080 -p 8080:8080 provider
PS 另一个(可以部署多个容器) 外网的:程序定的
docker run -d --rm --name provider-8081 -p 8081:8080 provider
Tips:
1. View log
docker logs -f --tail=500 provide-8080
2. Enter the container - it (interaction) followed by ID number
docker exec -it 96f93cc3b918 bash
3. Install VIM inside docker
During actual use, run apt get update, and then execute apt get install - y vim. Since the download address is an overseas address, the download speed is extremely slow and the update process may be interrupted, so do the following configuration
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
apt-get update
apt-get install vim
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码