简体   繁体   中英

Tomcat Docker Container: host webapp directory mounted as container webapp directory

I'm trying to deploy a Java Web application located in my Host OS (Windows 10) and I would like to mount the mentioned webapp as a directory under Tomcat Docker Container (location /usr/local/tomcat/webapps/mywebapp ).

I'm running the following command in the Docker terminal (absolute path for my webapp in Windows). Note: "mywebapp" host directory already contains the deployed/uncompressed war.

docker run --name=tomcat_7_0_78 --detach --publish 8080:8080 -v /c/desarrollo/workspace/webMavenTest/target/mywebapp:/usr/local/tomcat/webapps/mywebapp tomcat:7.0.78

I can see that Tomcat starts and the webapps Container folder has several default webapps inside it including the "mywebapp" folder. The problem is that it is empty so the webapp is not working.

I would like to use this to integrate my applications in the Eclipse IDE with Docker and achieve redeploying the application when I make changes through my IDE.

Any insight?

Thanks in advance.

If you want to a hot deploy every file changes, it is not possible to do it with a remote application server. The reason is that eclipse does not have control of the tomcat process to do it.

Now a possible workaround is to generate a webapp that generates a volume to a copy of the deliverable, so that after making a change you can do a build of the application and tomcat install it again.

It should look like this:

docker run --name = tomcat_7_0_78 --detach --publish 8080: 8080 -v /path/to/copy/war/app.war:/usr/local/tomcat/webapps/app.war tomcat: 7.0.78

And in your host you must make a copy of the "war" outside your project to the target of your application. This is necessary because when maven deletes the target folder the docker volume is disattached and does not refresh again.

To automate the deployment with plugins that perform hook over the save action:

How do I start Maven "compile" goal on save in Eclipse?

I do not recommend it, because you run maven all the time and it consumes a lot of cpu. Maybe write a local script to do it.

Thank you for your time and your answer, very helpful, I really appreciate it.

I expected an easier way to integrate this, but it seems there is no direct solution.

By the way, I have found out why the webapp directory was always empty in the Tomcat Docker Container, it seems that my webapp host path (Windows) must be under my user directory, something like this /c/users/username/documents/....../eclipseWorkspace/mywebappProject/target/mywebapp.

For the moment I think that I will enable remote debugging so I will have hot code replacement whenever I modify a Java class, and later see what could be done with other types of files.

About the problem you mention when maven deletes the target folder, I have checked that the webapp is disattached if I run a maven clean from Eclipse (as you said), but if I do a maven install it appears again within the Tomcat Docker Container, so maybe is not that bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM