Dockerfile for Tomcat and a Java project

Hi,

I have some Java project (authentication server, really doesn’t matter). It’s running on Tomcat. I’ve created a Dockerfile which can help me with both dev environment and production. But because I’m really new to this, I would like some professional review and I’ll appreciate any help.

This is my Dockerfile:

The whole idea is to run the image as

docker run -d -v $(pwd)/webapp/target:/opt/app {image_id} -name auth-server1

Then I’ll be able to build my java project and have changes right in my docker container. Everything works fine. But I need some review.

Thanks again in advance.

it’s usually a bad practice to have an sshd running. Try to just use docker exec if you need to access your containers for debugging. Mount volumes for normal access.

There is also another good practice to just run once process by container. You currently have the supervisord which starts multiple processes. This later will not scale and you cannot for example just fix one service (like the sshd) without touching another service (your tomcat for example).

Many people also try to explicitly specify the versions. So for example JAVA_MAJOR=8, JAVA_MINOR=123. That you can rebuild your image tomorrow and still use the same java version so you know it will work.

1 Like

Thanks a lot!

I’m sorry where did you find that supervisor manages multiple processes? My supervisor.conf has a record for catalina only.

who starts your sshd? supervisord is not needed. start the container with --restart=always if you want the service to be running every time.

sshd is not really needed. I added supervisor for the tomcat service. I will try with --restart=always option. Any drawback using supervisor? Because while I was looking in the Internet how to keep my tomcat running I came accross supervisor recommendations

supervisord just adds complexity. try to keep it as simple as possible.

1 Like

Had to keep asking because it’s still not clear. If I remove supervisor and just run
RUN /opt/tomcat-latest/bin/catalina.sh ru

My build process hangs on that line and I see in the output that tomcat server is running. I can send requests to the server,e verything works fine. But the problem is that it is during the build. Image itself is not created, so if interrupt it there is no way to run it.

Hello,

To launch tomcat directly, you have to use the CMD intruction of the Dockerfile :
CMD [“catalina.sh”,“run”]
(do not use the RUN instruction, it is only to install things needed by the final image during the build)

You can take a look at the official tomcat image :

And you can even use this official image instead of recreating a new one for your need :slight_smile:

1 Like

Thanks got it.

Regarding restart=always. I want to bring another example I pulled python:3.6.2 and run it with restart always option enabled. and guess what. I try to connect to it using

docker exec -it {container} bash

I’m getting

Error response from daemon: Container 26fb50ba5e2d0b29720538da86e6230cbbb64ed43564f7038abe68b1af2adc27 is restarting, wait until the container is running

Which shows that it runs stops starts in a loop which is pointless for a web server or even this python container case.

I know I asked too many questions, but I don’t want to give up on Docker. Please help and thanks in advance.

With python container I could overcome it by running container with -dit option but again for tomcat server continuously restarting doesn’t seem to be a good idea

the python image is not a thing which should run constantly. it just has the python interpreter. so it’s okay if you invoke your script and restart=always will restart your script then once it fails. You want to kind of use it more like a VM.

1 Like

Now I see. I shouldn’t use it as VM to exec the container and then run my script. Instead I should do something like

docker run {container} python {python_script}