outposts: give container time to boot when newly created

This commit is contained in:
Jens Langhammer 2020-10-16 23:37:44 +02:00
parent 8f08836885
commit c0ec6388df
1 changed files with 10 additions and 2 deletions

View File

@ -1,6 +1,8 @@
"""Docker controller""" """Docker controller"""
from time import sleep
from typing import Dict, Tuple from typing import Dict, Tuple
from django.conf import settings
from docker import DockerClient, from_env from docker import DockerClient, from_env
from docker.errors import DockerException, NotFound from docker.errors import DockerException, NotFound
from docker.models.containers import Container from docker.models.containers import Container
@ -58,6 +60,7 @@ class DockerController(BaseController):
detach=True, detach=True,
ports={x: x for _, x in self.deployment_ports.items()}, ports={x: x for _, x in self.deployment_ports.items()},
environment=self._get_env(), environment=self._get_env(),
network_mode="host" if settings.TEST else "bridge",
), ),
True, True,
) )
@ -65,8 +68,6 @@ class DockerController(BaseController):
def up(self): def up(self):
try: try:
container, has_been_created = self._get_container() container, has_been_created = self._get_container()
if has_been_created:
return None
# Check if the container is out of date, delete it and retry # Check if the container is out of date, delete it and retry
if len(container.image.tags) > 0: if len(container.image.tags) > 0:
tag: str = container.image.tags[0] tag: str = container.image.tags[0]
@ -94,13 +95,20 @@ class DockerController(BaseController):
): ):
# At this point we know the config is correct, but the container isn't healthy, # At this point we know the config is correct, but the container isn't healthy,
# so we just restart it with the same config # so we just restart it with the same config
if has_been_created:
# Since we've just created the container, give it some time to start.
# If its still not up by then, restart it
self.logger.info("Container is unhealthy and new, giving it time to boot.")
sleep(60)
self.logger.info("Container is unhealthy, restarting...") self.logger.info("Container is unhealthy, restarting...")
container.restart() container.restart()
return None
# Check that container is running # Check that container is running
if container.status != "running": if container.status != "running":
self.logger.info("Container is not running, restarting...") self.logger.info("Container is not running, restarting...")
container.start() container.start()
return None return None
return None
except DockerException as exc: except DockerException as exc:
raise ControllerException from exc raise ControllerException from exc