Keeping Docker container alive running Java application

Im having a recurring issue while trying to set up a Docker container so that it stays running.

Here is a sample of the Dockerfile that I am wanting to use:

  • boot2docker access elasticsearch on localhost
  • Docker compose wordpress plugins
  • If adding a command that repeats every 10 minutes to crontab, when does the first job run?
  • Windows Container - How to commit a container
  • iptables redirect to different host/tproxy in docker container
  • How can I install requirements.txt from docker-compose?
  • RUN wget -O /usr/local/nexus-2.11.3-01-bundle.tar.gz http://www.sonatype.org/downloads/nexus-2.11.3-01-bundle.tar.gz
    WORKDIR /usr/local
    RUN tar xvzf /usr/local/nexus-2.11.3-01-bundle.tar.gz
    RUN ln -s nexus-2.11.3-01 nexus
    
    ENV NEXUS_HOME /usr/local/nexus
    ENV RUN_AS_USER root
    CMD ["/usr/local/nexus/bin/nexus", "start"]
    
    EXPOSE 8081
    

    Basically when I build this, and then run it, the container just dies, and doing a docker ps command returns that there are no running containers.

    As far as I know, (please correct me if I’m wrong…) the docker container should stay running so long as theres a process with a pid of 1. Would the usage of the previous commands use PID 1, and if so, how can I force the nexus start command to use it? Or to just keep the container alive…

    The contents of a docker logs nexus gives:

    ****************************************
    WARNING - NOT RECOMMENDED TO RUN AS ROOT
    ****************************************
    Starting Nexus OSS...
    Started Nexus OSS.
    

    It seems to suggest that Nexus has started, but then again when I do a docker ps, I don’t see it running.

  • Failed to connect to master, spark in docker
  • jHipster fails to generate Gateway Docker Images
  • MySQL - Galera OR Kubernetes Replication Controller?
  • Docker Cloud Disable service endpoints
  • Jenkins wrong volume permissions
  • NGINX + PHP-FPM - Should I keep them in separate containers or not?
  • 2 Solutions collect form web for “Keeping Docker container alive running Java application”

    If the process running with PID 1 exits, then the container is automatically stopped. You can check on the sonatype/nexus repository here, using the concept of Launcher.

    Here is how they are avoiding the container to exit:

    ...
    RUN mkdir -p /opt/sonatype/nexus \
      && curl --fail --silent --location --retry 3 \
        https://download.sonatype.com/nexus/professional-bundle/nexus-professional-${NEXUS_VERSION}-bundle.tar.gz \
      | gunzip \
      | tar x -C /tmp nexus-professional-${NEXUS_VERSION} \
      && mv /tmp/nexus-professional-${NEXUS_VERSION}/* /opt/sonatype/nexus/ \
      && rm -rf /tmp/nexus-professional-${NEXUS_VERSION}
    RUN useradd -r -u 200 -m -c "nexus role account" -d ${SONATYPE_WORK} -s /bin/false nexus
    ...
    EXPOSE 8081
    WORKDIR /opt/sonatype/nexus
    USER nexus
    ENV CONTEXT_PATH /
    ENV MAX_HEAP 768m
    ENV MIN_HEAP 256m
    ENV JAVA_OPTS -server -XX:MaxPermSize=192m -Djava.net.preferIPv4Stack=true
    ENV LAUNCHER_CONF ./conf/jetty.xml ./conf/jetty-requestlog.xml
    CMD java \
      -Dnexus-work=${SONATYPE_WORK} -Dnexus-webapp-context-path=${CONTEXT_PATH} \
      -Xms${MIN_HEAP} -Xmx${MAX_HEAP} \
      -cp 'conf/:lib/*' \
      ${JAVA_OPTS} \
      org.sonatype.nexus.bootstrap.Launcher ${LAUNCHER_CONF}
    

    Since it is an open repository, you can directly refer to their repo, if you like.

    A quick guess from the logs is that running /usr/local/nexus/bin/nexus start would start it as a daemon.

    That would cause another process to spawn and the one that started the daemon would exit, terminating the container.

    One solution is to start the process not as a daemon, but I couldn’t find a option to do this in your nexus case.

    Another is to use something like supervisord as the CMD to docker. Then make it start your process.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.