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:

  • Shipyard v2 for Docker fails to login
  • Bind-mount a host directory into a volume of a running docker container
  • How to run sql script for postgres in docker
  • How to set the “current” directory of a Dockerfile with Docker Compose?
  • Transferring Docker run script to compose file - postgres doesn't run?
  • Container watches etcd leader vs container watches etcd cluster
  • 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.

  • docker: official ubuntu 14.04 image
  • Playframework unusual CPU load
  • Does restarting a Docker container “remember” initial run arguments?
  • Docker: execute a program that requires tty
  • Monitoring a log file for systemd service startup
  • Using fs.stat to get stats from a file inside a docker container directory
  • 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.