Search code examples
dockerdocker-composecontainersdocker-desktop

docker-compose container name use dash (-) instead of underscore (_)


I always used docker-compose on Ubuntu, in this environment containers are named with underscore:

  • <project>_<service>_<replica>

But now, I switched to Windows 10 (using Docker Desktop) and naming convention has changed:

  • <project>-<service>-<replica>

enter image description here

I don't know if this is OS dependent but it's a problem. My scripts are failing because they rely on containers named with underscores.

Is there a way to customize this and use underscore instead of dashes?


Solution

  • This naming convention difference appears to be a difference between Docker Compose versions v1 (Python) and v2 (Go). The latest docker/compose repo that is packaged with Docker Desktop is the golang version in the docker/compose v2 branch. Looking at the source code here in this branch:

    // Separator is used for naming components
    var Separator = "-"
    

    The python branch source code is using the _ naming convention for components, here for example:

        def rename_to_tmp_name(self):
            """Rename the container to a hopefully unique temporary container name
            by prepending the short id.
            """
            if not self.name.startswith(self.short_id):
                self.client.rename(
                    self.id, '{}_{}'.format(self.short_id, self.name)
                )
    

    As to solving this, you may want to uninstall the compose included with Docker Desktop and revert to a 1.28.x version. The compose readme says you can use pip install docker-compose to install. The compose docs have a section about upgrading this and commands to migrate to v2: https://docs.docker.com/compose/install/#upgrading but your question suggests you want to stay with the _ v1 naming convention.

    As mentioned in comments, the following options retain the compose compatibility:

    1. use --compatibility flag with docker-compose commands
    2. set COMPOSE_COMPATIBILITY=true environment variable

    Other doc links: