Restarting a Greengrass component with a container creates two instances of that container

0

I would like to restart a greengrass component, containing a docker container, without creating a second instance of that container. When I run the component restart command via the greengrass-cli, then run "docker container ls", it shows two active containers running.

Is it possible to restart a container component such that the original instance is killed ?

Container recipe lifecycle command pasted below:

{ "Run": "docker run --rm --net=host --runtime nvidia <ECR IMAGE> python3 <python script on container>" }

asked 2 years ago1109 views
2 Answers
0
Accepted Answer

You can restart the component and have it restart the container. Essentially Greengrass is sending a SIGTERM to the process running in the 'Run' portion of the lifecycle when stopping. In this case, the docker run command receives the signal. If the container can process the signal and exit gracefully, this will work. Here is more details on the process.

If the container cannot process the SIGTERM, the other way to mange is to use the Startup and Shutdown lifecycles within the recipe file. In this case, you can change your docker run to name the container and run in detached (background) mode. then when Greengrass stops the component, the Shutdown lifecycle would have a docker stop my_container_name which will complete the shutdown.

Using either approach will ensure that only one instance of the container is running. Note that there may be edge cases, so depending upon the situation, you may want to add additional logic to the recipe file to check for errant processes or running containers just like you would if do this directly from a shell script.

Hope this helps!

AWS
Gavin_A
answered 2 years ago
0

Thanks for the prompt reply, a few more clarifying questions.

Can the "Shutdown" lifecycle be used alongside the "Run" cycle, or does the use of "Shutdown" require that there may a "Startup".

If the component spawns the docker container in detached mode, will the docker output still appear in the greengrass component log as the docker runs?

answered 2 years ago
  • A component can have either Run or Startup paired with Shutdownlifecycle, so it's an either/or scenario.

    Component logs only include std* messaging (definitely stdout, pretty sure stderr to). So in detached mode, by default, docker logs would be need to be run at some point.

  • Hi! Checking in to see if this helped you for the container instances and logging.

  • FWIW: i've also added a docker kill and rm just before i start it up again.. that way if for some reason it didn't shutdown cleanly you won't have a failure to startup

    { "Run": " $(docker kill dockername || true) ; $(docker rm dockername || true); docker run --name=dockername ..... xxxxxxxx.dkr.ecr.ap-southeast-2.amazonaws.com/myimage:latest2 runsomestuff.sh" }

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions