Use terraform in docker, what happen when the container terminates or pauses

Hi all,

Does Terraform cover the following scenarios:

  1. Use Terraform in docker: while “terraform apply” is still running, I pause the container. Could Terraform update the remote or local state?

  2. Use Terraform in docker: while “terraform apply” is still running, I terminate the container. Could Terraform update the remote or local state?

I’m not quite sure what you are asking.

Terraform is just a standard application similar to any other things you might run (tools, web servers, databases, etc.). If you pause or terminate it or the thing it runs within (container, VM, etc.) it would stop running (temporarily or permanently).

In terms of the impact of pausing/killing Terraform while you are doing an apply it really depends on what it was doing at that moment.

The most likely issue is that the state file (stored locally or more commonly via remote state) will no longer be reflective of reality. An API call to a cloud provider could have just been made prior to the forced termination, meaning a new resource was created but not recorded in the state file. If you run terraform apply again it would try to create that resource again (which might fail due to conflict or create another instance of) as it is not aware of the one that was created.

If you just pause Terraform for a while and then let it continue the impact may be less. However you could still encounter problems such as credentials having expired or subsequent API calls failing due to that state Terraform was expecting to no longer be true.

1 Like

Many thanks, this is the answer I want to know. Actually I would like to use terraform in serverless custom container, but the container will be paused after several minutes, which causes that the remote state can’t be updated.

Hey @crazylnx how are you running terraform scripts in docker container??