Give a container a name
Give a container a fixed name with docker run --name and refer to it in later commands. A second container with the same name is refused until the first goes.
A container started in the background has a random generated name, and you want one you can type.
docker run -d --name demo-web alpine:3.20 sleep 60 >/dev/null
docker ps --filter name=demo-web --format '{{.Names}}'
docker run -d --name demo-web alpine:3.20 sleep 60 2>&1 | cut -c1-95
docker rm -f demo-webdemo-web
docker: Error response from daemon: Conflict. The container name "/demo-web" is already in use
Run 'docker run --help' for more information
demo-web--name sets the container's name, which then works anywhere an id does: docker logs, docker exec, docker stop. Names are unique on one daemon, so the second run is rejected, as the docker run reference implies. On a user-defined network the name is also the container's DNS name, so other containers reach it by it.
A name is held by the container even when it has stopped, so rerunning a script fails until docker rm removes the old one. Use docker rm -f first, or --rm so nothing is left. Compose adds a project prefix to names, so do not rely on a plain name there.