6

I am trying to build a docker container running tomcat from a docker file. Please find below the Dockerfile content:

FROM ubuntu:trusty
MAINTAINER karthik.jayaraman
VOLUME ["/tomcat/files"]
ADD /files/tar/apache-tomcat-7.0.47.tar.gz /usr/local/tomcat
ADD /files/scripts/. /tmp/tomcat_temp
RUN ls /tmp/tomcat_temp
RUN cp  -a /tmp/tomcat_temp/. /etc/init.d
RUN chmod 755 /etc/init.d/tomcat
RUN chkconfig --add tomcat && chkconfig --level 234 tomcat on
ADD /files/config   /usr/local/tomcat/apache-tomcat-7.0.47/conf/
ADD /files/lib  /usr/local/tomcat/apache-tomcat-7.0.47/lib/
ENV CATALINA_HOME /usr/local/tomcat/apache-tomcat-7.0.47
ENV PATH $PATH:$CATALINA_HOME/bin
EXPOSE 8080
CMD ["service","tomcat","start"]

When i create the image and run a bash in the container, with the command "Service tomcat start", the server is started. I checked the catalina.out file and ensured that its running. But when i try the host IP on which docker is installed and access the port using the port number 8080, i could connect to tomcat page. But when i specify the internal IP address of the container - 172.24.0.7:8080, i could view the tomcat page. I guess the port forwarding is not properly. Can someone tell me the error i am making here.

cucucool
  • 3,777
  • 8
  • 48
  • 63

1 Answers1

9

Your docker container is running as long as last command is not done. You are booting up your tomcat as a daemon. This makes docker to stop running container as soon as tomcat is started.

You can changed your last line to:

CMD service tomcat start && tail -f /var/lib/tomcat/logs/catalina.out

Or just try using one of precreated tomcat containers from Docker Hub: https://registry.hub.docker.com/search?q=tomcat&s=downloads

daniula
  • 6,898
  • 4
  • 32
  • 49
  • Thanks for your response. It worked. But when i give EXPOSE 8080:9999 in the Dockerfile and during container start if i give "docker run tomcat", i could not access the server using http://host-ip:9999 but if i start the container with "docker run -p 8080:9999 tomcat", i could access it. Any ideas why ? – cucucool Jun 16 '14 at 22:00
  • 1
    So how does your Dockerfile looks like, now? – daniula Jun 16 '14 at 22:11
  • EXPOSE 8080:9999 CMD service tomcat start && tail -f /usr/local/tomcat/apache-tomcat-7.0.47/logs/catalina.out In the place of current last two lines. – cucucool Jun 16 '14 at 23:04
  • 1
    Try with `EXPOSE 8080` instead of `EXPOSE 8080:9999`. – ivant Jun 16 '14 at 23:43
  • Ya it works. So does that mean only when i create a running container from the image, i should specify the port number to be forwarded like "docker run -p 8080:9999 image" - is that right. – cucucool Jun 17 '14 at 16:14