Skip to content

Support reuse of dev containers#1861

Open
nscuro wants to merge 1 commit intomainfrom
dev-container-reuse
Open

Support reuse of dev containers#1861
nscuro wants to merge 1 commit intomainfrom
dev-container-reuse

Conversation

@nscuro
Copy link
Copy Markdown
Member

@nscuro nscuro commented Mar 8, 2026

Description

Supports reuse of dev containers.

Allows database state to be reused across restarts, and more generally makes startup faster.

Addressed Issue

N/A

Additional Details

N/A

Checklist

  • I have read and understand the contributing guidelines
  • This PR fixes a defect, and I have provided tests to verify that the fix is effective
  • This PR implements an enhancement, and I have provided tests to verify that it works as intended
  • This PR introduces changes to the database model, and I have updated the migration changelog accordingly
  • This PR introduces new or alters existing behavior, and I have updated the documentation accordingly

@nscuro nscuro added this to the 5.7.0 milestone Mar 8, 2026
Copilot AI review requested due to automatic review settings March 8, 2026 17:14
@nscuro nscuro added the dx Issues related to developer experience (DX) label Mar 8, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for reusing Testcontainers-backed “dev services” containers across API server restarts, aiming to speed up local development by keeping stateful containers (e.g., PostgreSQL) running.

Changes:

  • Introduces a new configuration flag to enable/disable dev-services container reuse.
  • Configures PostgreSQL/Kafka/frontend dev-service containers with labels and withReuse(...).
  • Adjusts Kafka topic creation to tolerate existing topics when reusing containers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
apiserver/src/main/resources/application.properties Adds dev.services.container-reuse-enabled configuration and docs for container reuse.
apiserver/src/main/java/org/dependencytrack/dev/DevServicesInitializer.java Enables container reuse/labels, tolerates “topic already exists”, and conditionally skips stopping containers on shutdown.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1398 to +1404
# Defines whether dev services containers should be reused.
# <br/><br/>
# Refer to <https://java.testcontainers.org/features/reuse/> for details.
#
# @category: Development
# @type: boolean
dev.services.container-reuse-enabled=true
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new dev.services.container-reuse-enabled setting is documented, but it doesn’t mention two important behavioral constraints of Testcontainers reuse: (1) reuse is ignored unless TESTCONTAINERS_REUSE_ENABLE=true or testcontainers.reuse.enable=true is set, and (2) when reuse is enabled the containers are intentionally not disposed on shutdown. As written, this also conflicts with the earlier dev-services description that says containers are disposed when Dependency-Track stops. Please update the property docs (and/or the dev-services section above) to reflect the prerequisite and the changed shutdown behavior so users don’t end up with unexpected lingering containers.

Copilot uses AI. Check for mistakes.
Comment on lines 185 to 203
public void contextDestroyed(final ServletContextEvent event) {
if (postgresContainer != null) {
if (postgresContainer != null && !isContainerReuseEnabled) {
LOGGER.info("Stopping postgres container");
try {
postgresContainer.close();
} catch (Exception e) {
LOGGER.error("Failed to stop PostgreSQL container", e);
}
}
if (kafkaContainer != null) {
if (kafkaContainer != null && !isContainerReuseEnabled) {
LOGGER.info("Stopping Kafka container");
try {
kafkaContainer.close();
} catch (Exception e) {
LOGGER.error("Failed to stop Kafka container", e);
}
}
if (frontendContainer != null) {
if (frontendContainer != null && !isContainerReuseEnabled) {
LOGGER.info("Stopping frontend container");
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contextDestroyed() skips closing containers solely based on dev.services.container-reuse-enabled. In Testcontainers, withReuse(true) has no effect unless reuse is enabled globally (e.g. testcontainers.reuse.enable=true), so this can leave non-reusable containers running (and keep host ports bound) when the webapp is stopped/redeployed without a JVM exit. Consider only skipping close() when Testcontainers reports that the environment supports reuse, otherwise always close containers on shutdown.

Copilot uses AI. Check for mistakes.
Comment thread apiserver/src/main/java/org/dependencytrack/dev/DevServicesInitializer.java Outdated
Allows database state to be reused across restarts, and more generally makes startup faster.

Signed-off-by: nscuro <nscuro@protonmail.com>
@nscuro nscuro force-pushed the dev-container-reuse branch from 617dfcf to 0a7997f Compare April 18, 2026 16:05
@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dx Issues related to developer experience (DX)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants