After docker compose up --scale worker=3 there are three worker containers, and docker compose exec worker only ever lands in the first. The --index option chooses which one.
docker compose ps worker
docker compose exec --index 2 worker sh
The containers are numbered from 1, and the number is the suffix you see in their names, such as myproject-worker-2. So the first command shows which copies exist, and the second opens a shell in copy number 2.
I find it most useful when one copy behaves differently: it logs errors the others do not, or it is stuck on a job. Combine it with docker compose logs and the container name from ps to find the odd one out first.
The same --index option exists on docker compose cp, docker compose logs and docker compose port.
The numbers are not a stable identity. Scaling down removes copies and scaling up adds new ones, so look at docker compose ps each time rather than assuming that index 2 is still the container you were looking at yesterday.
Further reading: docker compose exec