invalid project name "My App": must consist only of lowercase alphanumeric characters, hyphens, and underscores as well as start with a letter or number — the name given to docker compose -p has a capital letter or a space. Use a name such as my-app and the command runs.

Write the project name in lower case and replace spaces with a hyphen or an underscore: docker compose -p my-app up. A top-level name: in the file behaved differently in this test; that case is covered below.

The error

PS> docker compose -p "My App" config
invalid project name "My App": must consist only of lowercase alphanumeric characters, hyphens, and underscores as well as start with a letter or number

The command exited with code 1. A name with capitals and no space was refused the same way:

PS> docker compose -p MyApp config
invalid project name "MyApp": must consist only of lowercase alphanumeric characters, hyphens, and underscores as well as start with a letter or number

Why it happens

The project name became the prefix of the names Compose gave in these tests: my-app_default for the default network in config, and fix10-cs-pn-web-1 for the container when the file was started with -p fix10-cs-pn. The message lists what the name may contain: lower-case letters, digits, hyphens and underscores, starting with a letter or a digit. "My App" breaks the rule twice, with capitals and a space, and "MyApp" once.

A top-level name: in the file did not give the error in this test. With name: My App and no -p, docker compose config exited with code 0 and printed name: myapp: Compose lower-cased the name and dropped the space without a word, and the default network became myapp_default, a prefix nobody typed.

PS> docker compose config
name: myapp
services:
  web:
...
networks:
  default:
    name: myapp_default

The fix

PS> docker compose -p my-app config
name: my-app
services:
  web:
...
networks:
  default:
    name: my-app_default

The same lower-case name works in the file, so the command line needs no -p at all:

name: my-app
services:
  web:
    image: alpine
    command: ["echo", "hello"]
PS> docker compose config
name: my-app
...
networks:
  default:
    name: my-app_default

Writing the name you want in the file, in the allowed form, means the prefix on the engine is the one you read in the file.

How it was reproduced

A one-service alpine file running echo hello, checked with docker compose config from Windows PowerShell 5.1 with -p "My App", -p MyApp, -p my_app and -p my-app, then started once with -p fix10-cs-pn and removed with docker compose down -v; then a second file with name: My App at the top and no -p, and a third with name: my-app, both checked with config only. Docker Compose v5.7.0, Docker Engine 29.9.3, Docker Desktop 4.99.0 on Windows (version 10.0.26300).

Frequently asked

What is a valid Docker Compose project name?
Lower-case letters, digits, hyphens and underscores, starting with a letter or a digit, as the error message itself says. my-app and my_app are valid; My App and MyApp are not.
Why does docker compose -p reject my project name?
It contains a capital letter, a space or another character outside the allowed set. In this test both "My App" and "MyApp" were refused with the invalid project name error.
Why are my containers named myapp when the file says name: My App?
In this test Compose accepted name: My App in the file without an error and turned it into myapp, lower case with the space removed. Write the name in the allowed form to get the prefix you expect.

More decoded errors in the Fixes category.