Useful docker commands

Useful docker commands

Docker is a powerful tool to develop modern application softwares. I myself use docker all the times to develop, deploy and sometime debug my applications. Docker ships out of the box a bunch of handful commands which really make a server maintaining process at scale convenient. However, it is not easy for one to remember all the commands or sometimes just do not know which command to use. I ran into a situation where I deployed a stack, container failed and automatically get deleted. I could not figure out reasons until I knew a simple command. I was frustrating, then I think it is worth to share some of  fundamental commands I use the most often, and hopefully can save some of your time.

Basic Docker Commands

  1. docker build -t {tag-name} -f {docker-file-path} . build an image from a Dockerfile
  2. docker run --detach --rm --name {container-name} -p {port}:{port} {image nane} deploy an application with an image built from previous command. Those "detach", "rm" and "name" flags are optional, sometimes I get rid of "detach" to debug a program
  3. docker logs {container-name} show all container logs
  4. docker ps list all running containers
  5. docker ps -a list all containers including running and already dead
  6. docker stop {container-name} stop a running container
  7. docker rm {container-name} remove a container (you need to stop it first)
  8. docker rm $(docker ps -a -q) remove all dead container
  9. docker rmi {image-name} remove an image (if it is not associated with a container)
  10. docker rmi $(docker images -f "dangling=true") remove all images that are not associated with a container
  1. docker stack deploy -c {composer-file-path} {stack-name} deploy a stack defined by a *.yml file
  2. docker service ls list all stack service in order to investigate service statuses
  3. docker service rm {stack-service-name} kill a stack
  4. docker stack rm {stack-name} remove 1 service from a stack
  5. docker service ps {service-name} list a service information including state, previous deployment, error status, node that service is running on
  6. docker service ps --no-trunc {service-name} list a service information including state, previous deployment, error status, node that service is running. Messages are fully displayed.
  7. docker service logs {service-name} show logs inside of a service container, it is useful to debug docker errors
  8. docker node ls list all stack nodes
  9. docker node ps {node-name} examine the node status, be able to see which services are running on this node without having to login to the node
  10. docker node demote demote a manager to be a worker
  11. docker node promote promote a worker to be a manager
  12. docker node rm remove a node from stack
  13. docker stats docker overall stats including cpu usage, memory usage, network io.

That is all I use, you can look for more on docker official website. Happy Dockering!!!