The following situation:
- I have a Spring Boot Application
- which runs in a Docker swarm
- but fails to start because it was not properly configured (a property is missing).
- It seems to me that the docker swarm always tries to restart the container, but always fails because of the missing property.
- The restart makes no sense because docker will never be able to start the application unless I fix the missing property.
- So currently the swarm ends in an endless loop.
Regarding this problem I already read:
- The docker documentation: https://docs.docker.com/config/containers/start-containers-automatically/
- and several StackOverflow posts: https://stackoverflow.com/search?q=Docker+restart
My “setup”: The dockerfile:
ARG nexus_docker_registry=mynexus.com:10099
FROM ${nexus_docker_registry}/openjdk:8-jdk-alpine
ADD myjar.jar myjar.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "java", "-jar", "/myjar.jar" ]
my YML-file to create the docker service:
---
- hosts: docker_manager
become: false
vars:
servicename: 'myservice'
imageurl: "mynexus.com:10099/myjar:{{version}}"
extraoptions:
- "--with-registry-auth"
- "--detach=true"
- "--log-driver gelf"
- "--log-opt 'gelf-address=udp://{{ groups['logstash'][0] }}:10001'"
- "--hostname 'myhost.com'"
- "--mount 'type=bind,source=/etc/localtime,destination=/etc/localtime:ro'"
- "--mount 'type=volume,source=mykeys,destination=/mykeys'"
- "--env 'spring.profiles.active=docker'"
- "--publish 8000:6666"
tasks:
- name: Include vault
include_vars: "myvault.yml"
- name: "delete service '{{ servicename }}'"
command: sudo docker service rm "{{ servicename }}"
args:
warn: false
ignore_errors: true
run_once: true
- name: "create service {{ servicename }}"
command: sudo docker service create {{ extraoptions | join( ' ' ) }} --name "{{ servicename }}" "{{ imageurl }}"
args:
warn: false
run_once: true
What I want to achieve is:
- If the spring boot application is not able to start because of for example a
BeanCreationException
or something similar, then I don’t want the docker service to restart endlessly. - If I restart the swarm etc. the docker service should restart automatically.
In the docker documentation is written:
If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop.
So I guess that what I want to achieve is not possible with a restart policy.
Questions:
- but maybe I can write something in my
Dockerfile
that I achieve my goals? - Or am I totally wrong here and misinterpret the documentation?
I am unfortunately not a docker expert and still learning to handle ‘the swarm’.
Advertisement
Answer
Because it does not seem possible what I wanted to achieve, I read the documentation again (https://docs.docker.com/engine/reference/commandline/service_create/) and found the option --restart-max-attempts
which will solve my problem with the endless loop.