- Docker is a set of PaaS (platform as a service) product that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.
- Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
- An image is responsible for setting up the application and environment that will exist inside the container. The important thing to understand is that images are read-only. You do not make edits to an image; you make edits to the Dockerfile and then build a new image.
- The container is what actually runs our app. Think of the container like an isolated Linux box. It’s essentially a lighter weight virtual machine. The point of having a container is standardization. An application only has to care about the container it’s run in.
- No more, “But it works on my machine!” If an app runs in a container, it will work the same way no matter where the container itself is hosted. This makes both local development and production deployments much, much easier.
docker build -t image-name:tag .
docker run -p 5000:8080 <image-id>
- Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not tagged or associated with a container):
docker system prune
- To additionally remove any stopped containers and all unused images (not just dangling images), add the
-aflag to the command:
docker system prune -a
- Use the
docker imagescommand with the-aflag to locate the ID of the images you want to remove. This will show you every image, including intermediate image layers. When you’ve located the images you want to delete, you can pass their ID or tag todocker rmi:
List:
docker images -a
Remove:
docker rmi Image Image
- Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space. They can be located by adding the filter flag
-fwith a value ofdangling=trueto thedocker imagescommand. When you’re sure you want to delete them, you can use thedocker image prunecommand:
Note: If you build an image without tagging it, the image will appear on the list of dangling images because it has no association with a tagged image. You can avoid this situation by providing a tag when you build, and you can retroactively tag an image with the docker tag command.
List:
docker images -f dangling=true
Remove:
docker image prune
- You can find all the images that match a pattern using a combination of
docker imagesandgrep. Once you’re satisfied, you can delete them by usingawkto pass the IDs todocker rmi. Note that these utilities are not supplied by Docker and are not necessarily available on all systems:
List:
docker images -a | grep "pattern"
Remove:
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
- All the Docker images on a system can be listed by adding
-ato thedocker imagescommand. Once you’re sure you want to delete them all, you can add the-qflag to pass the image ID todocker rmi:
List:
docker images -a
Remove:
docker rmi $(docker images -a -q)
- Use the
docker pscommand with the-aflag to locate the name or ID of the containers you want to remove:
List:
docker ps -a
Remove:
docker rm ID_or_Name ID_or_Name
- If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run
docker run --rmto automatically delete it when it exits:
Run and Remove:
docker run --rm image_name
- You can locate containers using
docker ps -aand filter them by their status:created,restarting,running,paused, orexited. To review the list ofexitedcontainers, use the-fflag to filter based on status. When you’ve verified you want to remove those containers, use-qto pass the IDs to thedocker rmcommand:
List:
docker ps -a -f status=exited
Remove:
docker rm $(docker ps -a -f status=exited -q)
- Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either
created(a state which can result when you run a container with an invalid command) orexited, you can use two filters:
List:
docker ps -a -f status=exited -f status=created
Remove:
docker rm $(docker ps -a -f status=exited -f status=created -q)
- You can find all the containers that match a pattern using a combination of
docker psandgrep. When you’re satisfied that you have the list you want to delete, you can useawkandxargsto supply the ID todocker rm. Note that these utilities are not supplied by Docker and are not necessarily available on all systems:
List:
docker ps -a | grep "pattern”
Remove:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
- You can review the containers on your system with
docker ps. Adding the-aflag will show all containers. When you’re sure you want to delete them, you can add the-qflag to supply the IDs to thedocker stopanddocker rmcommands:
List:
docker ps -a
Remove:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
- Use the
docker volume lscommand to locate the volume name or names you wish to delete. Then you can remove one or more volumes with thedocker volume rmcommand:
List:
docker volume ls
Remove:
docker volume rm volume_name volume_name
- Since the point of volumes is to exist independent from containers, when a container is removed, a volume is not automatically removed at the same time. When a volume exists and is no longer connected to any containers, it’s called a dangling volume.
- To locate them to confirm you want to remove them, you can use the
docker volume lscommand with a filter to limit the results to dangling volumes. When you’re satisfied with the list, you can remove them all withdocker volume prune:
List:
docker volume ls -f dangling=true
Remove:
docker volume prune
- If you created an unnamed volume, it can be deleted at the same time as the container with the
-vflag. Note that this only works with unnamed volumes. When the container is successfully removed, its ID is displayed. Note that no reference is made to the removal of the volume. If it is unnamed, it is silently removed from the system. If it is named, it silently stays present.
Remove:
docker rm -v container_name