'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>
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 1:[1]
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:
- use
--compatibility
flag withdocker-compose
commands - set
COMPOSE_COMPATIBILITY=true
environment variable
Other doc links:
- Backward compatibility is discussed here: https://github.com/docker/compose#about-update-and-backward-compatibility
- Docs/Readmes suggest installing compose-switch for translating docker-compose commands
- If you instead want to upgrade to v2 on Ubuntu and modify scripts: https://docs.docker.com/compose/cli-command/#install-on-linux
Solution 2:[2]
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:
Solution 3:[3]
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.
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 expect. - 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.
Solution 4:[4]
We noticed that the command
docker compose up
and
docker-compose up
would give either _ or - depending on which one you used.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | jawira |
Solution 3 | |
Solution 4 | Adrian Sweeney |