How to avoid Docker container deletion after build completion

Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.

Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. 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

Summary

When running containers through Docker Tasks in Bamboo, by default, all containers created during a Bamboo Job execution will be deleted after its completion. Depending on your use-case scenario, you might want to keep the containers running after the build completes, for troubleshooting purposes for example. This article will go through the steps to achieve that.

Environment

Bamboo Build Plans with Docker Tasks.

Diagnosis

The log snippet below shows that the container is run by the Docker Task, but gets removed by the post-build plugin Docker Container Cleanup after the job finishes.

simple 24-May-2022 18:41:36 Starting task 'Docker run' of type 'com.atlassian.bamboo.plugins.bamboo-docker-plugin:task.docker.cli' simple 24-May-2022 18:41:36 Running image name (busybox:latest) [...] build 24-May-2022 18:41:36 16fc6cb175c44457c8c059f30674b18d30a3463d4bdebc7d807f5b1826e21b0c simple 24-May-2022 18:41:36 Finished task 'Docker run' with result: Success simple 24-May-2022 18:41:36 Running post build plugin 'Docker Container Cleanup'

Cause

By design, all containers started in the background by the Docker Tasks during the execution of a job will be terminated at the end of the job.

You can use the Detach container checkbox to start a container in a task in detached mode, and then use that same container in subsequent tasks (e.g., to run tests). However, the container will still be removed once the build job has finished.

Detached container checkbox

(Auto-migrated image: description temporarily unavailable)

The Container name must be unique. Please note that the job will fail if there's a container already running with the same name. You can use a variable, like ${bamboo.buildNumber} to work around that.

If you're looking for a way to start a container and keep it running after the Bamboo job and its respective build have been completed, please refer to the Solution section.

Solution

Option 1: Script Tasks

Run the Docker command using a Script Task, and add the -d option for detached execution.

Example: Script Task content

docker run --name=bamboo8 --init -d -p 54663:54663 -p 8085:8085 atlassian/bamboo-server:latest

Option 2: Modify the default behavior of the Docker Plugin bundled with Bamboo

There is a way to disable the "Docker Container Cleanup", which is the post-build plugin that removes the containers. It's a module of the bundled Bamboo Docker Support Plugin that can be disabled through the following steps:

  1. Log in as a Bamboo administrator

  2. Click the "Cog" icon >> "Overview"

  3. Open "Manage Apps"

  4. In the drop-down menu, select "All apps"

  5. Search for "Bamboo Docker Support"

  6. Click "4 of 4 modules enabled"

  7. Scroll a little further down to "Docker Container Cleanup"

  8. Click "Disable"

    (Auto-migrated image: description temporarily unavailable)

By doing that, the post-build plugin won't be executed anymore, therefore, the containers will not be deleted once the build finishes.

Please note that the container name must be unique. The job will fail if there's a container already running with the same name (e.g., from a previous build run). You can use a variable, like ${bamboo.buildNumber}  to work around that.

Updated on May 22, 2025

Still need help?

The Atlassian Community is here for you.