docker exec -it my-container sh
-i keeps stdin open, -t gives you a terminal. Use bash if the image has it; Alpine-based ones usually only have sh.
With compose you use the service name instead of the container name:
docker compose exec app sh
To run a single command and leave, drop the -it and the shell:
docker exec my-container cat /etc/hosts