How to make sure detached containers are removed once the build stops

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

The steps outlined on this article are provided AS-IS. This means we've had reports of them working for some customers — under certain circumstances — yet are not officially supported, nor can we guarantee they'll work for your specific scenario.

You may follow through and validate them on your own non-prod environments prior to production or fall back to supported alternatives if they don't work out.

We also invite you to reach out to our Community for matters that fall beyond Atlassian's scope of support!

Summary

Bamboo Build Plans can be used to run Docker containers both through Script Tasks or Docker Tasks. However, depending on the environment and use case, the containers might keep running after the build finishes, or if the build has been stopped halfway through.

Environment

Bamboo Build Plans with Script Tasks that run Docker containers.

Solution

Docker Tasks

You can use Bamboo's native Docker Tasks to run your containers. The containers will be automatically removed upon task completion by the Docker Container Cleanup module of the Docker Plugin bundled with Bamboo. Please see Configuring the Docker task in Bamboo documentation for more information.

However, if the containers need to be created by a Scrip Task for your use case scenario, you may resort to the following workarounds to make sure the containers are removed after the build completes (or if it's stopped midway through).

Workaround 1: final task

Add a Script Task to remove all containers created by your previous scripts, and configure that task as a Final Task, meaning it will always run, even if the build is manually stopped or if the build fails:

Configuring tasks documentation

Final tasks will run sequentially, once the build tasks have completed. Final tasks will always be executed, regardless of whether any Build tasks or other Final tasks fail. Final tasks will be executed even if you stopped the build manually.

This is useful if you don't want leftover containers from failed builds.

Workaround 2: manual stage

You could use the "Manual Stages" feature in your Bamboo Plan to achieve something similar to a manual "Delete containers" button. 

  1. On your Build Plan, add a Manual Stage at the end of the build configuration. Manual Stages are not run by default and require a user to trigger them manually.
  2. Inside that Manual Stage, add a Script Task that deletes the containers created by your previous scripts.

Given that the manual stage will not run by default, you can use this workaround to delete the containers only when you really need them to be removed. This might be useful for troubleshooting scenarios where you'd want them to keep running so you can check their logs or other information before removing them.

Please note that in order for the container clean-up to work when implemented using a Manual Stage, the deletion job inside of it needs to run on the same agent environment that ran the job that created the containers. You can achieve that by using Agent Dedication or Capabilities and Requirements.

Last modified on Jun 8, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.