How to avoid Docker container deletion after build completion
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
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 (see below) 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.
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.
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:
- Log in as a Bamboo administrator
- Click the "Cog" icon >> "Overview"
- Open "Manage Apps"
- In the drop-down menu, select "All apps"
- Search for "Bamboo Docker Support"
- Click "4 of 4 modules enabled"
- Scroll a little further down to "Docker Container Cleanup"
Click "Disable"
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.