Docker-deploy a Java project

You may not be outstanding, but you must be different

Docker deploy Java project

Server operation based on Centos7.6

Docker deployment jar package project

Deploy by packaging into a mirror

1. Create a folder

Pass in the jar package type project

2. Create a Dockerfile

Insert picture description here

3. Edit the Dockerfile

#FROM命令定义构建镜像的基础镜像,该条必须是dockerfile的首个命令
FROM java:8
#用于指定持久化目录
VOLUME /temp
#将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问网络资源,类似wget
ADD schoolinfo.jar schoolinfo.jar
#构建镜像时执行的命令
RUN bash -c 'touch /schoolinfo.jar'
#配置容器,使其可执行化。配合CMD可省去"application",只使用参数
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Xmx500m","-Xms500m","/schoolinfo.jar"]

4. Start building the image

docker build -t test/schoolinfo .

5. View the docker image

docker images

6. Use the docker image to run the container

docker run -d -p 8081:8081 --name schoolinfo test/schoolinfo
[[email protected] ~]# cd /root/jarfile
[[email protected] jarfile]# docker build -t test/schoolinfo .
Sending build context to Docker daemon   72.3MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : VOLUME /temp
 ---> Running in 32012e9ed635
Removing intermediate container 32012e9ed635
 ---> fe3f4e1fe03a
Step 3/5 : ADD schoolinfo.jar schoolinfo.jar
 ---> 648fbcf5c901
Step 4/5 : RUN bash -c 'touch /schoolinfo.jar'
 ---> Running in 7d0e8f2c9b06
Removing intermediate container 7d0e8f2c9b06
 ---> 3c35f77022d8
Step 5/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Xmx500m","-Xms500m","/schoolinfo.jar"]
 ---> Running in 8b4436b7bbe5
Removing intermediate container 8b4436b7bbe5
 ---> 203ba233c236
Successfully built 203ba233c236
Successfully tagged test/schoolinfo:latest
[[email protected] jarfile]# docker run -d -p 8081:8081 --name schoolinfo test/schoolinfo
fcfa1e65a71cc1603137d512f698cffab6df7c514cb884085af8912ae24024d3
[[email protected] jarfile]# 

Use java -jar to run project deployment

1. Upload the jar package project that needs to be deployed to the centos server

2. Upload the jar package to home. Here I upload schoolinfo.jar to home.

3.kdvolder/jdk8 is the mirror image of my jdk8

Self-install

docker pull kdvolder/jdk8

4. Run with docker

[[email protected] ~]# docker run -d --restart=always  -p 8081:8081 -v /home/schoolinfo.jar:/usr/schoolinfo.jar  --name schoolinfo kdvolder/jdk8  java -jar /usr/schoolinfo.jar
8142c6b16b3986d0887a70e0bab6cdafb4acec01ce9daf5e95a24e6a7cd741c4

Precautions:

There may be two reasons for the inaccessibility of the project after successful startup

1. The mysql or redis or rabbitmq in the jar package project cannot be accessed if localhost is used. It is recommended to use the public network ip

2. The firewall of the server may have been turned on, or the port may not be open