2

To start a Docker container I ran the command docker compose up -d. (Actually, the -d switch is unimportant.)

docker ps shows the following containers are running.

CONTAINER ID   IMAGE                            COMMAND                  CREATED         STATUS         PORTS                               NAMES
678ef93d18dc   python-my-python-project         "python3 -m my_pytho…"   6 seconds ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   python-my-python-project-1

Where do the image and container names come from? How are these strings generated?

Image Name: python-my-python-project
Container Name: python-my-python-project-1

Contents of Dockerfile:

# syntax=docker/dockerfile:1

from python:3.12-bookworm
workdir /python-my-python-project
copy ./my_python_project ./my_python_project
copy ./requirements.txt .
run pip3 install --no-cache-dir --upgrade -r requirements.txt
cmd ["python3", "-m", "my_python_project"]
expose 80

Contents of docker-compose.yml:

services:
    python-my-python-project:
        build: .
        ports:
            - "80:80"

I haven't been able to find any documentation about how the names are created. I understand that the name should not be manually specified in either the Dockerfile or docker-compose.yml specification because manually pinning the name causes problems when trying to deploy containerized applications as part of a swarm.

What "rule" causes the name python-my-python-project to be generated?

3
  • 1
    Compose specification does not specify how the container name is generated, but Docker Compose names the container after the service name, which is python-my-python-project in your case. See github.com/docker/compose/blob/…
    – Violet
    Commented Jul 8 at 21:12
  • @Violet want to add that as an answer? Commented Jul 8 at 21:39
  • The container names I see are <project name>-<service name>-<instance number>. Project name can be set with the -p option on compose commands and defaults to the current directory name. Service name and instance name are pretty self explanatory. This is with compose 2.19.1. Commented Jul 8 at 22:16

1 Answer 1

4

The exact format of this has varied between implementations of Compose. For the current Docker-plugin Compose tool version 2, the rule for an image name comes from a function called GetImageNameOrDefault: it consists of the Compose project name, a hyphen, and the specific Compose service name. A similar rule applies for the generated container names, including a replica number for replicated containers. (Python-based Compose version 1 used underscores in these names.)

This set of rules means a couple of things:

  • You pretty much never need to manually specify container_name: since Compose can generate a unique name. Most of the docker CLI commands have docker compose equivalents that are able to look up services by name in the docker-compose.yml file.
  • You don't need to manually specify image: when you also specify build:, unless you're specifically planning to push the image to a registry.
    services:
       something:
         build: .
         image: registry.example.com/project/something:${SOMETHING_TAG:-latest}
    
  • You don't need to include the project name as part of the Compose service name, because Compose includes it on its own in the generated names. Short names like db or app will be unambiguous in the context of this specific Compose project.

Not the answer you're looking for? Browse other questions tagged or ask your own question.