A container was started with docker run -itd … bash. You open a shell in it with docker exec -it <name> bash and type tty. Why does it print /dev/pts/1 and not /dev/pts/0?
/dev/pts/0 is already taken by the container's main process, the bash started with -it; exec -it starts a second process with its own new pseudo-terminal, numbered 1.
-t allocates a pseudo-terminal (pts), and the container numbers them from 0, just as every new terminal tab on a Linux desktop gets the next number. In this case:
docker container run -itd …starts the container's main process (bash, PID 1) attached to the first pseudo-terminal,/dev/pts/0. With-dnobody is looking at it, but it exists and stays open.docker container exec -it … bashdoes not join that process. It starts an additional bash inside the same container and allocates a fresh terminal for it,/dev/pts/1.
That is also the practical difference between exec and attach: docker attach connects you to the main process on pts/0, so typing exit there ends PID 1 and the container stops. exit in an exec shell only ends that extra shell, and the container keeps running.
Go deeper:
man7.org: pty(7) — how Linux pseudo-terminals and /dev/pts work.
Docker Docs: docker container exec — exec starts a new process in a running container.