51

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?

jawira
  • 3,668
  • 2
  • 17
  • 27

4 Answers4

68

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:

cam
  • 4,409
  • 2
  • 24
  • 34
  • 7
    Great answer, thanks to you I found that `--compatibility` flag maintains the v1 behavior: `docker-compose --compatibility up -d` – jawira Oct 11 '21 at 08:17
  • 6
    You can also use `COMPOSE_COMPATIBILITY=true` environment variable instead of `--compatibility` option. – jawira Oct 11 '21 at 08:26
  • 1
    @jawira glad it's helpful and I'll add those options the answer for completeness – cam Oct 11 '21 at 08:44
  • 1
    If you need to preserve the env var suggested above for using with `sudo` you can do `--preserve-env=COMPOSE_COMPATIBILITY` after doing the `COMPOSE_COMPATIBILITY=true` – amurrell Nov 03 '21 at 01:36
  • Since I already started the service and had a container with hyphens, I had to `rm` the container for `--compatibility` to work. Stopping it wasn't enough. – Noumenon Nov 28 '22 at 07:52
  • I specified the container name in my `docker-compose .yml` using `container_name: project-db` to fix this problem. – trouble_bucket Jan 21 '23 at 14:51
  • 1
    In current v2 branch you can see that the separator was moved to api.go: https://github.com/docker/compose/blob/v2/pkg/api/api.go#L546 – c0d3b34n May 15 '23 at 10:44
12

After the last Docker Desktop update, "docker-compose" automatically downgraded to v1.

Now there is a new option to select your "docker-compose" version, which was disabled by default:

enter image description here

jawira
  • 3,668
  • 2
  • 17
  • 27
6

Just to save people from insanity: Docker Desktop packaging appears to be glitchy.

I am running Docker Desktop 4.3.0 (71786) on Windows 11, with the WSL2 backend.

docker desktop

Here are the docker-compose information from my terminal:

% which docker-compose
/usr/bin/docker-compose
% ls -al /usr/bin/docker-compose
lrwxrwxrwx 1 root root 56 Dec   9 19:52 /usr/bin/docker-compose -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker-compose
% sha256sum /usr/bin/docker-compose
35dd89af6820dc111991f4c9b8ed7b253fefa33a30392fed506490ddc2553e91 /usr/bin/docker-compose
% docker-compose --version
Docker Compose version v2.2.1

Docker Desktop has been freshly installed. With this version, _ is used as the separator character.

Now, let's switch to the same version of docker-compose, downloaded from GitHub:

% sudo wget -q https://github.com/docker/compose/releases/download/v2.2.1/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose-v2.2.1-gh
% sudo chmod 755 /usr/local/bin/docker-compose-v2.2.1-gh
% sudo ln -sf /usr/local/bin/docker-compose-v2.2.1-gh /usr/bin/docker-compose
% ls -al /usr/bin/docker-compose
lrwxrwxrwx 1 root root 39 Dec   9 20:18 /usr/bin/docker-compose -> /usr/local/bin/docker-compose-v2.2.1-gh
% sha256sum /usr/bin/docker-compose
68a3bb67bd25abf0f6da2a9d171a873ac182c3cc1c51fb4866e7770675823d2f  /usr/bin/docker-compose
% docker-compose --version
Docker Compose version v2.2.1

With the GitHub version, - is used as the separator character, as expected.

This means that Docker doesn't just repackage the binaries available on GitHub, but make their own builds from a source tree that is different from the one tagged with the same version on GitHub. (Thanks @tentative for pointing out the line of code defining the separator!)

And some final notes:

  • docker-compose from other recent versions of Docker Desktop used - as expected.
  • It is not the first time that switching between - and _ happened. Anecdotally, I remember having the very same issue a few months back, but after upgrading it went away.
  • If you don't destroy your environment, docker-compose up will use the existing container names. This means that it may take you a while to notice the change.
  • I have reported this to the GitHub Issue Tracker of Docker for Windows. As of end of 2022, the issue has been fixed.
m000
  • 5,932
  • 3
  • 31
  • 28
1

We noticed that the command

docker compose up and docker-compose up

would give either _ or - depending on which one you used.

  • 5
    That's because these are 2 different commands : `docker-compose` (with a dash) was a third-party tool, now integrated into Docker as a subcommand `docker compose` (with a space). These are two different implementations, which have a different convention. – Lenormju May 19 '22 at 11:42