Docker Cheatsheet


I use Docker day to day. So I decided to make a cheatsheet on Docker.

Stop, Enable and Start Docker

sudo systemctl stop docker, sudo systemctl enable docker, sudo systemctl start docker

List all the running containers

docker ps

Stop a container

docker stop CONTAINER_NAME/ID

Kill a container

docker kill CONTAINER_NAME/ID

The difference between Kill and Stop command is that it will stop the container gracefully if container has the ability to stop itself gracefully after getting the stop command. Kill command will stop the command immediately.

List all the images

docker images -a

Remove one specfic image

docker rmi imageID1 imageID2

List all exited containers

docker ps -aq -f status=exited

Remove stopped containers.

This command will not remove running containers, only an error message will be printed out for each of them. Remove dangling/untagged images

docker ps -aq --no-trunc -f status=exited | xargs docker rm

Remove dangling/untagged images

docker images -q --filter dangling=true | xargs docker rmi

Remove containers created after a specific container

docker ps --since a1bz3768ez7g -q | xargs docker rm

Remove containers created before a specific container

docker ps --before a1bz3768ez7g -q | xargs docker rm

How to remove intermediary images during docker build

Use --rm together with docker build to remove intermediary images during the build process.

To get in to the Container

docker exec -it CONTAINER_NAME bash

If I want to edit a file inside Container

First I copy the file in the docker hosted machine. docker cp CONTAINER_NAME:/path/to/file.ext . Then I edit the file locally using your favorite editor, and then copy back again to the container to replace the old file. docker cp file.ext CONTAINER_NAME:/path/to/file.ext

How to remove and clean up local docker containers and images

docker system prune 
[Purging All Unused or Dangling Images, Containers, Volumes, and Networks]

docker system prune -a
[remove any stopped containers and all unused images, not just dangling images]

docker image prune 
[Remove dangling images]

docker rmi $(docker images -a -q) 
[Remove all images]

docker rm $(docker ps -a -f status=exited -q) 
[Remove all exited containers]

docker rm $(docker ps -a -f status=exited -f status=created -q) 
[Remove containers using more than one filter]

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
[Stop and remove all containers]

docker rm -v container_name
[Remove a container and its volume]

Post a Comment

0 Comments