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
docker build -t {tag-name} -f {docker-file-path} .
build an image from aDockerfile
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 programdocker logs {container-name}
show all container logsdocker ps
list all running containersdocker ps -a
list all containers including running and already deaddocker stop {container-name}
stop a running containerdocker rm {container-name}
remove a container (you need to stop it first)docker rm $(docker ps -a -q)
remove all dead containerdocker rmi {image-name}
remove an image (if it is not associated with a container)docker rmi $(docker images -f "dangling=true")
remove all images that are not associated with a container
Docker Stack Related Commands
docker stack deploy -c {composer-file-path} {stack-name}
deploy a stack defined by a*.yml
filedocker service ls
list all stack service in order to investigate service statusesdocker service rm {stack-service-name}
kill a stackdocker stack rm {stack-name}
remove 1 service from a stackdocker service ps {service-name}
list a service information including state, previous deployment, error status, node that service is running ondocker 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.docker service logs {service-name}
show logs inside of a service container, it is useful to debug docker errorsdocker node ls
list all stack nodesdocker 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 nodedocker node demote
demote a manager to be a workerdocker node promote
promote a worker to be a managerdocker node rm
remove a node from stackdocker 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!!!