Allow for existing containers (allow sharing containers across services)
Problem
I think it would be useful if compose could allow you to specify containers that may already exist, eg: [code block] If the container 'shareddata' did not exist, it would be created as usual. If, however, that container already existed, the allow_existing: true setting would not complain about a duplicate container, instead just skipping creation (and perhaps it would try to bring the container up if it were stopped?). I haven't python-ed in a long time, but I might be able to create a PR for this feature if someone wanted to give me a little guidance on where the best place to start looking into the code would be.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Allow Existing Containers Feature in Docker Compose
Docker Compose currently does not support the ability to skip the creation of containers that already exist, leading to errors when attempting to create a container with the same name. This limitation restricts the ability to share containers across different services, which can be useful for managing shared resources like databases or caches.
Awaiting Verification
Be the first to verify this fix
- 1
Modify Docker Compose Configuration
Add a new configuration option 'allow_existing' to the Docker Compose YAML schema. This option should be a boolean that indicates whether to skip the creation of a container if it already exists.
yamlversion: '3' services: shareddata: image: myimage allow_existing: true - 2
Update Container Creation Logic
In the Docker Compose codebase, locate the section responsible for container creation. Modify the logic to check for the existence of a container with the specified name. If 'allow_existing' is true and the container exists, skip the creation step.
javascript// Pseudocode for checking existing container if (allow_existing && containerExists(containerName)) { // Skip creation } else { createContainer(containerName); } - 3
Implement Container Restart Logic
If the container already exists but is stopped, implement logic to restart the container. This can be done by checking the container's status and using the Docker API to start it if it is not running.
javascript// Pseudocode for restarting a stopped container if (containerExists(containerName) && isContainerStopped(containerName)) { startContainer(containerName); } - 4
Add Unit Tests for New Feature
Create unit tests to validate the new 'allow_existing' functionality. Ensure that the tests cover scenarios where the container exists, does not exist, and is stopped.
javascriptdescribe('Allow Existing Containers', () => { it('should skip creation if container exists', () => { // Test logic here }); it('should restart the container if stopped', () => { // Test logic here }); });
Validation
To confirm the fix worked, deploy the modified Docker Compose version and create a service with the 'allow_existing' option set to true. Verify that if the container already exists, it is not recreated, and if it is stopped, it is restarted successfully.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep