Docker Tips

Docker is an open platform for distributed applications. It’s portable and lightweight. Once you’re dealing with Docker, then you’ll be figuring out how to do some tasks. In this post I will show you some of them.

Managing containers

Containers are easy to create. Everytime you do docker run then a new container will be created. For seeing all active containers that you have, you can use docker ps.

Sometimes you want to display all containers that you have, so there is docker ps -a comes to play. Too much information presented? you can just display the numeric IDs only by using docker ps -a -q.

If you have too much containers, you can also do filtering on it. By passing flag -f KEY=VALUE, you have filter controls. For example, if you want to filter by container by its name called “goofy_wozniak”, you can use docker ps -a -f name=goofy_wozniak. Want to filter inactive containers? you can simply do docker ps -a -q -f status=exited.

By combining filtering above, we can build a command to remove unused containers, like this:

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

For stopping running containers, there is docker stop command. This is basically send a SIGTERM signal then a SIGKILL signal after grace period. For forcing stop, there is also docker kill which send a SIGKILL immediately to the running containers.

Running containers

After running a docker container, say:

$ docker run busybox echo "hello world"

You usually figuring out what is the ID of the container. Fortunately, you can use docker ps -l -q. Usually, you need to store the value somewhere else so you can use that later. You can use good old way of assigning variable for that case:

$ ID=$(docker ps -l -q)

Once you have the ID on variable $ID, then you can use that for inspecting container. For example, for getting the IP address of the container:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' $ID
172.17.0.4

You can also stop the container by using docker stop $ID, and pass additional flag -f when you want to force stop the container.

What about re-running the container after I stop it? Easy, there is a docker start for it.

Container ID and name are interchangeable. Use one of them as per your taste.

Managing images

Images management is bit similar to containers one. Instead of using docker ps, you need to use docker images. Most commonly used images filtering is dangling=true, which can be used for displaying all untagged images, that are the leaves of the images tree.

Removing images is using docker rmi. It’s expected to receive parameters image IDs. You can also passing image tag to bulk delete:

$ docker rmi ubuntu:{trusty,precise}

When you decide to remove all images on your system, then a handy command, docker rmi $(docker images -q) comes as a rescue.

Conclusion

Basically all of the command line techniques are covered on the Docker CLI documentation. You need to read through that to fully understand how docker’s CLI works. All the things that are not covered by the documentation are spread out on the internet, including this blog post.