Simple But Useful Docker Commands

Uğur Akgül
3 min readNov 5, 2019
Simple docker commands.

Most of us are using Docker as a containerization tool. Some features in Docker are easy to use but seems complex in action. Based on real life experiences , I have decided to write about this easy to use commands.

In this story I will put together simple but powerful Docker commands. Let’s start.

Image Commands

Docker is about images. If you want something to run inside a container , then you need to make an image about it. This section will provide useful commands for docker images.

  • Build images

docker build <Dockerfile_path>

  • Show all images

docker images -a

  • Delete an image

docker rmi <image_id>

Important note: If you want to delete an image that has been used by a stopped container , system won’t let you delete it. You can either force the delete or you can remove the stopped container and continue deleting existing image. You can use -f flag for forcing.

docker rmi -f <image_id>

  • If you have tagged an image when you are building it , you can delete it with it’s tag. You don’t need to know about image ID.

docker rmi <repository_name:tag_name>

  • If you didn’t provide a tag to an image when building it , the image becomes a dangling image. To remove all dangling images

docker images purge

Container Commands

Well , containerization maybe the most used technology nowadays. There are some commands that needed the most about a container. Let’s see.

  • Start container

docker run <image_id>

docker run <repository:tag>

If you don’t have an image for the container , docker will try to download it from Docker Hub by default.

  • Start container in detached mode

docker run -d <image_id>

docker run -d <repository:tag>

  • Start container in interactive mode

docker run -it <image_id>

docker run -it <repository:tag>

You can specify the shell in command by writing shell type in command. If you don’t specify container will use default shell. For example;

docker run -it <image_id> /bin/bash

  • Execute a command in working container

docker exec -d <container_id> <command>

docker exec -d <container_name> <command>

For example ;

docker exec -d ubuntu_bash touch /tmp/exectemp

This command will create a exectemp file in /tmp directory of ubunt_bash container in detached mode.

docker exec -it ubuntu_bash touch /tmp/exectemp

And this command will create a exectemp file in /tmp directory of ubuntu_bash container in interactive mode.

  • Stop working containers

docker stop <container_id>

  • Show exited containers

docker ps -a -f status=exited

In this command -a means “all” and -f means “filter”. We are filtering all containers about their status.

  • Delete stopped containers

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

This command is needed for some occasions. For example , as I mentioned above , if you are trying to remove an image that has been used by a stopped container , you can remove all stopped containers with this command.

  • Delete all unused resources (image, container , network , volume) in docker system.

docker system prune

  • In addition to deleting all unused resources , delete all stopped containers.

docker system prune -a

A valuable note: There is a command named docker inspect. This command will show you any information about a container. For example it’s IP address , network , image , volumes , ports etc.

Of course docker inspect command alone is hard to read. It returns JSON. It contains all the information about that container and you may need only the IP address or binded volumes etc.

In this case you can use format flag to get only what you need. With format flag you pick any field from JSON pretty straightforward.

docker inspect — format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ <container_id>

Conclusion

There we are , these are the most common commands in Docker. Of course I might be missed some of commands but I will be writing about my experiences. To be fair , we don’t use Docker alone because in microservices or in a work with too many containers , nobody can track those containers’ states. Containers can stop and restart , and in every restart it can get a different IP address. We couldn’t track IP addresses individually. For this (and many more) purpose we use an orchestration tool like Kubernetes or Swarm. But that’s another days story.

Thank you for reading. See you soon :)

--

--

Uğur Akgül

Cloud Engineer at TurkNet İletisim Hizmetleri // DevOps Engineer // You can find me at https://www.linkedin.com/in/hikmetugurakgul/