[SOLVED] Copy files into a Tomcat image

I would like to copy the contents of the extracted .war into “webapps ROOT”.
What should I add to this image?
https://hub.docker.com/_/tomcat
I’m satisfied with the free service, I’m a student.
I know the COPY command but I need other tools that I don’t know if Docker provides.
Thank you

As you have didn’t provide details about your use case, it leaves room for wild assumption… or kills the impulse to respond at all.

What is it what you want?
– build a new image containing the war
– mount the war into a container
– hot deploy from your ide, e.g. eclipse
– something entirly different

I would like to use Docker to test a .war file that I produced with IntelliJ, an IDE similar to Eclipse.
The problem is that I don’t know how to copy the .war into the image.
With this form:
https://hub.docker.com/_/httpd
I wrote this:
COPY ./htdocs/ /usr/local/apache2/htdocs/
I don’t understand how to behave with Tomcat.
On the VPS that I test I add the .war extracted to:
tomcat/webapps/ROOT
Before doing this I delete all the files from ROOT.

Applies to all of those:

Please be more specific and on point!

I added the string “COPY” at the bottom of the Dockerfile.

		exit 1; \
	fi

EXPOSE 8080
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

Then I typed this:

D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat-9-java-11:v2.0 .
docker run -it --rm --name tomcat-9-java-11-container -p 8888:8080 tomcat-9-java-11:v2.0

The browser sends me another link and I can’t see the index of my site.
https://192.168.99.103:8443/
Also to be able to use my webapp without problems, I should:
1.Cancel the ROOT content;
2.In ROOT put my unzipped .war file.
On the server I solved with points 1 and 2.
On the server I can see the index even if I don’t use ROOT but this link:
webapps/application-name
Where am I wrong?
Did I understand exactly how to use Dockerfile?
I made other attempts with this code but I don’t resolve.

D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker stop tomcat-9-java-11-container
docker rm tomcat-9-java-11-container
docker rmi tomcat-9-java-11:v2.0
docker build -t tomcat-9-java-11:v2.0 .
docker run -it --rm --name tomcat-9-java-11-container -p 8888:8080 tomcat-9-java-11:v2.0

Thank you

I show you another solution that does not work:

Dockerfile

...
EXPOSE 8080
CMD ["cd /usr/local/tomcat/webapps/"]
CMD ["mv ROOT ROOT.old"]
CMD ["mkdir ROOT"]
COPY ./esercitazione.1.maven/ /usr/local/tomcat/webapps/ROOT/
CMD ["catalina.sh", "run"]

Windows prompt

D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker stop tomcat-9-java-11-container
docker rm tomcat-9-java-11-container
docker rmi tomcat-9-java-11:v2.0
docker build -t tomcat-9-java-11:v2.0 .
docker run -it --rm --name tomcat-9-java-11-container -p 8888:8080 tomcat-9-java-11:v2.0

Browser

http://192.168.99.103:8888/

What I see:

It looks like you did everything correct except that your app is redirecting to port 8443 from 8080 and you didn’t forward that port. From everything I’ve read in your posts, your Dockerfile should be something like this:

FROM tomcat:9-jre11
EXPOSE 8080 8443
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

Then built and run it with:

docker build -t esercitazione .
docker run --rm -p 8080:8080 -p 8443:8443 esercitazione

Then if your app redirects from 8080 to 8443 both ports are forwarded.

1 Like

Thanks for the reply.

My application is a simple webapp made in java using trivial jsp.
I don’t use the https protocol so I don’t understand why I have to open that port too.
The only clarification I have to make is relative to the ROOT folder. Basically my application only works when I’m in ROOT. In practice, to see the home of the site I have to unpack my application in ROOT.
I tried this alternative code but it doesn’t work anyway:

FROM tomcat:9-jre11
EXPOSE 8080 8443
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
COPY ./esercitazione.1.maven/ webapps/ROOT/
CMD ["catalina.sh", "run"]

Then I built and run it with:

docker build -t esercitazione .
docker run --rm -p 8080:8080 -p 8443:8443 esercitazione

Of course I also tried your code but it doesn’t work.

Inside the browser I write:

http://192.168.99.103:8080/
https://192.168.99.103:8443/

Do you have a .war file? If you do, you could rename it to ROOT.war, and copy it to the `/usr/local/tomcat/webapps’ directory. Your Dockerfile would be something like this:

FROM tomcat:9-jre11
RUN mv /usr/local/tomcat/webapps/ROOT /usr/local/tomcat/webapps/ROOT.old
COPY ./ROOT.war /usr/local/tomcat/webapps/

You don’t need the EXPOSE or CMD lines, because those are unchanged from the base image tomcat:9-jre11.

Build and run it with the same commands you already use. Then browse port 8080.

1 Like

OK, then your war needs to be copied as ROOT.war as @rajchaudhuri pointed out, so the proper Dockerfile would then be:

FROM tomcat:9-jre11
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war

(substitute whatever the “real” name of your war file is if it’s not esercitazione.1.maven.war)

That will deploy your app as the root application. @rajchaudhuri is correct that you don’t need EXPOSE or CMD because those are inherited. My Dockerfile just renames as it copies but it essentially the same.

1 Like

I tried your Dockerfile but the browser sends me to https://192.168.99.103:8443/
I read in the browser:
Unable to reach site Connection denied by 192.168.99.103.
Try to:
Check the connection
Check the proxy and firewall
ERR_CONNECTION_REFUSED
The war file that I use locally is identical to the one I use on the Ubuntu VPS. I just don’t understand why your code doesn’t work. Your code is perfect.

I tried your Dockerfile but the browser sends me to https://192.168.99.103:8443/
I read in the browser:
Unable to reach site Connection denied by 192.168.99.103.
Try to:
Check the connection
Check the proxy and firewall
ERR_CONNECTION_REFUSED
I just don’t understand why your Dockerfile doesn’t work.

You have been magnificent! Your help has been essential.
I was able to write the correct Dockerfile:

FROM tomcat:9-jre11
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]

To create the image I use this code:

docker build -t esercitazione:v.2.0 .
docker run -it --rm -p 8888:8080 esercitazione:v.2.0

I write this in the browser:

http://192.168.99.103:8888/

I’m very happy!

Glad that worked! :slight_smile:

1 Like

What exactly does this command do?

RUN rm -fr /usr/local/tomcat/webapps/ROOT

The “rm” command removes ROOT but “-fr” what exactly does it do?

Found:

The -r means recursive and -f means force. So rm -fr is remove by force recursively .

If you just rm ./ROOT it would complain that ROOT is a directory.

If you are removing a directory/folder, you must add recursive and force so that it deletes the entire contents of the folder (regardless of how deep it goes) and doesn’t complain that it’s not empty.

1 Like

I would like to change my code so that the testing process of my .war applications is faster. I edited the code this way and now the server has stopped working.
For what reason?

FROM tomcat:9-jre11
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]

docker build -t tomcat:v.9.0.17 .
docker run -d --name tomcat-container -p 8888:8080 tomcat:v.9.0.17

Does the asterisk mean that the .war file can have any name?
Is it wrong to use the asterisk instead of the file name?

The following command works:

docker run -it --rm -p 8888:8080 esercitazione:v.2.0

but does not allow the user to use the shell again to write other code. I would like to use something like this:

docker run -d --name tomcat-container -p 8888:8080 tomcat:v.9.0.17

The code above can be used or with Tomcat is it an error?

P.S.: I tried to use the old code again but it doesn’t work anymore. How is it possible? Why does this happen?!?!?! I’m really disheartened. I deleted all the images and containers from Docker and then recreated everything using the working codes but I didn’t solve it.

Code:
https://paste.ubuntu.com/p/r6RQMxqsnp/
Browser:
http://192.168.99.103:8888
The browser sends me to this address without displaying any page:
https://192.168.99.103:8443/
https://paste.ubuntu.com/p/svK7882M5g/
This thing that is happening to me is shocking!
I feel like crying.
Could it be some side problem that does not reside in the Docker code?
Are all PCs certified to work with Docker?
Is there a way to find out?
I am a student, I had to work hard to buy an i3-8100.

I installed Linux Ubuntu 18.04 on VirtualBox then I installed Docker and in the end I made a second attempt:
https://paste.ubuntu.com/p/dyj5RXQNdK/
Unfortunately the speech does not change. I write this:
http://localhost:8888/
and the browser sends me here:
https://localhost:8443/
Browser message translated into English:
https://paste.ubuntu.com/p/V4BGcvfFM2/
Original browser message:
https://paste.ubuntu.com/p/tg7rdGk3kF/
I do not know what to think…