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:

  • How to access Docker web app from host OS?
  • Get CPU/Memory percentage of a docker machine
  • Setting up jupyterhub docker using one of the jupyter stacks
  • how to execute docker commands through Java program
  • Error “No module named MySQLdb” on docker
  • Docker restart entrypoint
  • RUN wget -O /usr/local/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:

    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.

  • what about the docker image if a container is crashed?
  • Can't connect Ngxin reverse proxy to Gunicorn on Django container (Docker)
  • docker host rebooted automatically when running docker build/run
  • Dockerfile issues with build
  • Docker: Push data container onto Docker Hub
  • Digital ocean kill the build process on docker
  • 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 \${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 MAX_HEAP 768m
    ENV MIN_HEAP 256m
    ENV JAVA_OPTS -server -XX:MaxPermSize=192m
    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/*' \

    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.