Before using the jar package deployment, a problem with the jar package deployment is that the jar package is not decompressed, and it is inconvenient to modify the configuration file when locating the on-site problem. So it feels better to deploy in the form of a war package. Then you need to do the following things:
1. Modify the pom.xml file and startup class of each module
(1) Change the packaging method to war package
(2) Exclude built-in tomcat and introduce servlet api dependency
<!--Introduce spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--Exclude built-in tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--Introduce servlet api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency>
(3) The main function startup class needs to inherit SpringBootServletInitializer and rewrite the config method
@SpringBootApplication @EnableEurekaServer public class ServerApplication_First extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ServerApplication_First.class); } public static void main(String[] args) { new SpringApplicationBuilder(ServerApplication_First.class).web(true).run(args); } }
2. Prepare the tomcat base image
You can pull the official tomcat image in docker-hub, or you can customize the tomcat image based on the ubuntu image.
If you customize the tomcat image, you need:
(1) First download jdk and tomcat
- server-jre-8u51-linux-x64.gz
- apache-tomcat-8.0.24.tar.gz
(2) Create a new Dockerfile with the following contents:
FROM ubuntu
ADD server-jre-8u51-linux-x64.gz /usr/local
ADD apache-tomcat-8.0.24.tar.gz /usr/local
RUN mv /usr/local/apache-tomcat-8.0.24 /usr/local/tomcat
ENV CATALINA_HOME /usr/local/tomcat
ENV JAVA_HOME /usr/local/jdk1.8.0_51
EXPOSE 8080
ENV PATH $PATH:$JAVA_HOME/bin
- (3) Build an image
$ docker build -t tomcat .
3. Make a project mirror
Pay attention here, because our multiple containers are associated with the links parameter of docker-compose (the method is to set the domain name resolution of /etc/hosts). So if the tomcat ports of multiple containers are all 8080, there will be conflicts. So you need to open different ports for tomcat in each container.
After trying, we can use the following method (overwriting the original image setting.xml when generating a new image) to achieve the goal:
(1) First, we need to get the setting.xml file of the basic image tomcat (to prevent different versions of tomcat):
Mount the host directory using interactive mode:
docker run –name tomcat -it -v /data/:/data/ tomcat /bin/bash
Where -v is the mount directory, that is, the first /data/ is the host's second /data/ is the container image
After entering the interactive mode, copy the server.xml file in the conf to the data directory, so that we can get the server.xml file of the mirror tomcat from the host.
(2) After getting this file, you can modify the custom port (modify yourself).Transform the Dockerfile:
# Based on which mirror FROM tomcat # Indicate the maintainer of the mirror yyx MAINTAINER "yyx<[email protected]>" # Add the war file to the image and rename it to athena.war ADD *.war /usr/local/tomcat/webapps/athena.war # Overwrite the original image file with the custom port file ADD server.xml /usr/local/tomcat/conf # run tomcat via shell script CMD ["catalina.sh","run"]
4. After that, you can build and run these images through the docker build command or the docker-compose configuration file.