If you want to do it in multiple, easy-to-remember commands:
- list stopped containers:
docker ps -a
- copy the name or the container id of the container you want to attach to, and start the container with:
docker start -i <name/id>
The -i
flag tells docker to attach to the container's stdin.
If the container wasn't started with an interactive shell to connect to, you need to do this to run a shell:
docker start <name/id>
docker exec -it <name/id> /bin/sh
The /bin/sh
is the shell usually available with alpine-based images.
If you're having problems with the container exiting immediately when you start it above, you can re-run it with an interactive shell with the following. You need the name of the image here,
not the container. Because restarting didn't work, the only way to debug the problem is to delete and run it again. You are put into a shell, where you can try the CMD from the Dockerfile to see its output, or to debug why it is exiting immediately.
docker rm <name/id>
docker run -it --entrypoint /bin/sh <image-name> -s