This has saved me more times than I can count, having the ability to debug a running container the way you would in a "normal" VM.
First, see which containers are running;
$ docker ps CONTAINER ID IMAGE COMMAND [...] NAMES 925cc10d55df 66cc85c3f275 "gitlab-runner-ser..." [...] runner-f500bed1-project-3888560-concurrent-0-mysql-0-wait-for-service 0ab431ea0bcf 3e3878acd190 "docker-entrypoint..." [...] runner-f500bed1-project-3888560-concurrent-0-mysql-0 4d9de6c0fba1 nginx:alpine "nginx -g 'daemon ..." [...] nginx-container
To get a shell (Bash) on a container of choice, run this;
$ docker exec -i -t nginx-container /bin/bash
nginx-container determines which container you want to enter, it's the name in the last column of the
docker ps output.
Alternatively, use the container ID;
$ docker exec -i -t 4d9de6c0fba1 /bin/bash
docker attach, as that'll give you funky results if the initial command that's started in a Docker container is something like MongoDB or Redis, the instance will be killed.