The default naming scheme for containers created by Compose
Problem
> The default naming scheme for containers created by Compose in this version > has changed from `<project>_<service>_<index>` to > `<project>_<service>_<index>_<slug>` Is there any way to disable this behavior apart from using `container_name` in yaml file? We have many scripts that rely on container names and are not using swarm, just a single container stack. This change is very inconvenient for us.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Revert to Previous Container Naming Scheme in Docker Compose
The change in the default naming scheme for containers created by Docker Compose from `<project>_<service>_<index>` to `<project>_<service>_<index>_<slug>` was introduced to enhance container identification in more complex setups. However, this change can disrupt existing scripts and workflows that rely on the previous naming convention, especially in single container stacks.
Awaiting Verification
Be the first to verify this fix
- 1
Create a Custom Naming Scheme
Modify your Docker Compose file to specify a custom naming scheme for your containers. This can be done by using the `container_name` directive within each service definition to set the name explicitly.
yamlversion: '3' services: my_service: image: my_image container_name: my_project_my_service_1 - 2
Update All Service Definitions
Ensure that all services in your Docker Compose file have the `container_name` specified. This will prevent the new naming convention from being applied and maintain consistency across your scripts.
yamlversion: '3' services: service1: image: image1 container_name: my_project_service1_1 service2: image: image2 container_name: my_project_service2_1 - 3
Test Container Naming
After updating the Compose file, run the `docker-compose up` command to recreate the containers. Verify that the container names match the expected format by using the `docker ps` command.
bashdocker-compose up -d docker ps - 4
Update Scripts to Use New Names
If you cannot use `container_name`, update your scripts to accommodate the new naming scheme by parsing the container names dynamically based on the project and service names.
bashproject_name=$(basename $(pwd)) container_name="${project_name}_service1_1" echo $container_name
Validation
Confirm that the container names are as expected by running `docker ps` and checking that the output matches the specified `container_name` values. Additionally, test your existing scripts to ensure they function correctly with the updated naming.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep