Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
jawira Avatar asked Oct 06 '21 10:10

jawira


People also ask

Can docker image names have hyphens?

A docker tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods, and dashes. A tag name may not start with a period or a dash and may contain a maximum of 128 characters.

How do I rename a docker compose container?

To rename a docker container, use the rename sub-command as shown, in the following example, we renaming the container discourse_app to a new name disc_app. After renaming a containers, confirm that it is now using the new name. For more information, see the docker-run man page. That's all!

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.

Does docker tag overwrite?

Currently, if you deploy an image with a user, then try to push the image again (to overwrite it) with no delete/overwrite permissions, the docker client returns a successful message. If you try to overwrite the image/tag with a different image, then there will be an error.

Why is docker-compose-up changing the name of my container?

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. would give either _ or - depending on which one you used.

What is the difference between docker compose V1 and V2?

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:

Why does docker compose discard labels with none as value?

Docker Compose discards com.docker.compose.filepaths labels that have None as value. This usually occurs when labels originate from stdin. Added OS X binary as a directory to solve slow start up time issues caused by macOS Catalina binary scan. Passed the HOME environment variable in container mode when running with script/run/run.sh.

What is dash in Docker?

Running Dash in Docker This is part one of a short series of posts about Dash. The repository for this blog posts is here. Dash is an application framework to build dashboards (hence the name) or in general data visualization heavy largely customized web apps in Python.


Video Answer


3 Answers

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:

  • 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
like image 93
tentative Avatar answered Oct 19 '22 19:10

tentative


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

like image 8
jawira Avatar answered Oct 19 '22 19:10

jawira


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 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.
like image 4
m000 Avatar answered Oct 19 '22 19:10

m000