Docker defence against the dark arts

I've been working quite a bit with Docker these days and have found a few commands/tricks that I'm always looking up and have saved in my notes that tend to help me.

This post details a few situations I've found and how I managed to get through it. I'll keep adding sitations and commands as I encounter them.

Attach to a running Docker container

This is a weird one. Docker includes a "attach" command but all it seems to do is attach to the Standard streams and not allow you to do anything.

So you might go to:

docker ps

And get the Container ID then run:

sudo docker attach 665b4a3e17b6

Which will result in your terminal just hanging ...

But I have since found that the exec command allows you to actually execute a command in the container like you would expect.

To get into bash for the container shown above, you'd run:

sudo docker exec -i -t 665b4a3e17b6 bash

Note that you can also run against the name of the container, for example:

sudo docker exec -i -t loving_gazelle bash

Docker using all my disk space

You SSH into your server to build a new Docker image and then suddenly realize that the servers' disk space has been DESTROYED. WHAAT, you ask yourself ...

This happens because of "dangling"images that were made during previous builds and never deleted.

I've found numerous commands but the easiest and what appears to be cleanest way would be:

docker rmi $(docker images -q --filter "dangling=true")

This command will delete all images that have no "tag" defined.

Another options is to run:

docker rm -v $(docker ps -a -q -f status=exited)

which will remove all your older containers that might still be hanging around using disk space even after they are gone.

--

Ways I've read to avoid this situation is to create a mount just for Docker which it can use and fill up without affecting the system itself. Not quite what I'd like to hear either but the approach works.

Missed something ?

Let me know in the comments below and I'll update to start building a nice resource of docker commands / tricks.