4

I have docker image which I create from my docker file. When I run the image it has able to run the tomcat server then the command prompt come back. That mean the process is terminated and I think the container stops. So when I see http://localhost:8080 no tomcat page is appear. So I am not able find actual what is the problem. I am actually trying to to build custom java8, tomcat8 and maven as environment and I want to deploy my maven project in that tomcat server. Bellow is the Dockerfile to create image

FROM scratch
FROM ubuntu:16.04

RUN mkdir /opt/java8
RUN mkdir /opt/tomcat8
RUN mkdir /opt/maven3

ENV JAVA_HOME /opt/java8
ENV CATALINA_HOME /opt/tomcat8
ENV M2_HOME /opt/maven3

ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$M2_HOME/bin

ADD jdk1.8.0_112 /opt/java8
ADD apache-tomcat-8.0.38 /opt/tomcat8
ADD apache-maven-3.3.9 /opt/maven3

EXPOSE 8080

CMD ["startup.sh", "run"]

I put 3 folder of java, tomcat, maven near Docker file so those are added.

Now when I build the image and run the image the bellow log appear.

root@dhavalbhoot:/home/veni/Documents/dhaval_bhoot/docker_images/tomcat1#
  docker run -it -p 8080:8080 dhaval/tomcat:8.0.38

Output:

Using CATALINA_BASE:   /opt/tomcat8
Using CATALINA_HOME:   /opt/tomcat8
Using CATALINA_TMPDIR: /opt/tomcat8/temp
Using JRE_HOME:        /opt/java8
Using CLASSPATH:       
  \#/opt/tomcat8/bin/bootstrap.jar:/opt/tomcat8/bin/tomcat-juli.jar
Tomcat started.

root@dhavalbhoot:/home/veni/Documents/dhaval_bhoot/docker_images/tomcat1#

This way the prompt come back and I check in browser http://localhost:8080 tomcat page not appear So help me solving the problem.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Dhaval Bhoot
  • 241
  • 1
  • 5
  • 18
  • 1
    I am confused: why make the formatting of your question worse? I had properly formatted the output of docker run. – VonC Jun 16 '17 at 06:30
  • Can I restore the proper format? That will make your question more readable. – VonC Jun 16 '17 at 06:33
  • I have restored the format, just for you to see what I meant: if you don't like it, you can reverse my edit. – VonC Jun 16 '17 at 06:40

1 Answers1

4

A similar docker official tomcat image (8.0.40) runs:

CMD ["catalina.sh", "run"]

With catalina.sh made to start tomcat in the foreground: the process won't exit immediately.
If you tomcat installation does include that script, you should use it instead or startup.sh.

Or run directly a tomcat image for testing:

$ docker run -it --rm -p 8080:8080 tomcat:8.0

You can test it by visiting http://container-ip:8080 in a browser

Qasim Sarfraz
  • 5,822
  • 2
  • 20
  • 26
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @DhavalBhoot I suspect you have `/opt/java8/jdk1.8.0_112/bin/java` instead of directly `/opt/java8/bin/java`. Instead of starting from `SCRATCH` or from `Ubuntu`, why not starting your image from `tomcat:8.0`? That way, you don't have to ADD anything: the right java is already present. – VonC Jun 16 '17 at 06:37
  • Adding CMD ["catalina.sh", "run"] instead of CMD ["startup.sh", "run"] solve the problem and process won't exit. Now can you help me to run this process in background and also how can I install all these 3 things using tar.gz from their official sites? – Dhaval Bhoot Jun 16 '17 at 06:42
  • @DhavalBhoot Which 3 things? If your image strart from tomcat, it would already have tomcat and java. – VonC Jun 16 '17 at 06:43
  • right now I add 3 things java, tomcat and maven using folder, means I have put those 3 folder next to my Dockerfile and write ADD command to add them in image. Now I want to get those from the website of those software in form of tar.gz. I use "FROM scratch" because I want to create this as base image, then in future I will create one another Dockerfile in which I give the reference of the previous image which has java, tomcat, and maven and put next to my project code. then I add those code in image and want to run "mvn tomcat:deploy" command which will deploy the code in tomcat. – Dhaval Bhoot Jun 16 '17 at 06:59
  • @DhavalBhoot which docker version are you running? What you describe looks like a multistage build, with Docker 17.05 (meaning with a recent version of Docker, you no longer need 2 Dockerfiles: you can do everything with one Dockerfile, using 2 "FROM" images: read more at https://docs.docker.com/engine/userguide/eng-image/multistage-build/. – VonC Jun 16 '17 at 07:02
  • I can say this in simple word as, I want to build .war file and deploy in tomcat in container, so I just want to build one image file and run it, all the things from environment setup means install of software and then building of .war file from code and deployment in tomcat. So this everything run in one container – Dhaval Bhoot Jun 16 '17 at 07:04
  • @DhavalBhoot But to your original question: It is best to ADD directly the tar.gz or war files instead of the folder. Add them in a folder you have declared in your Tomcat server.xml: see http://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/ – VonC Jun 16 '17 at 07:07
  • I have Docker version 17.03.1-ce, build c6d412e – Dhaval Bhoot Jun 16 '17 at 07:07
  • @DhavalBhoot with 17.05, you could have one Dockerfile, with one first part using an image like maven, in which you have already java and maven and which you can use to build your war. The second part of the Dockerfile would use the result of the first image, and copy the war to your tomcat in the webapps folder, as described in https://stackoverflow.com/q/5109112/6309 – VonC Jun 16 '17 at 07:09
  • How I write the maven command in Dockerfile which build the war file and add that in image? I think I have to execute the maven command on my local machine, so how to write those in Docker file. For example I want to execute "mvn install" then how I write it in Docker file? – Dhaval Bhoot Jun 16 '17 at 07:29
  • @DhavalBhoot No you can execute any command you want in a Dockerfile, with the RUN directive. – VonC Jun 16 '17 at 07:29
  • Ok, thank you for helping me, let me explore more in "multi-stage builds" also, now if I have further question then I will ask here. If you can provide some kind of complete tutorial then it will be very help full, I am java developer and new to Docker. Thank you @VonC – Dhaval Bhoot Jun 16 '17 at 07:36
  • I try to build war file so I add the command in Dockerfile RUN cd /home/veni/git/M_UserTP RUN mvn install RUN mv .../target/M_UserTP.war .../tomcat1 ADD M_UserTP.war /opt/tomcat8/webapps Then I make docker build and get the error /bin/sh: 1: cd: can't cd to /home/veni/git/M_UserTP The command '/bin/sh -c cd /home/veni/git/M_UserTP' returned a non-zero code: 2 – Dhaval Bhoot Jun 16 '17 at 10:20
  • @DhavalBhoot At this point, it is best to ask a new question with your new Dockerfile: that will be easier to answer there than in a series of comments – VonC Jun 16 '17 at 10:30