I use Docker day to day. So I decided to make a cheatsheet on Docker.
Stop, Eable and Start Docker
sudo systemctl stop docker, sudo systemctl enable docker, sudo systemctl start docker
List all the running containers
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