Docker
Docker simplifies app deployment by packaging programs with their environment. It includes files, env vars, dependencies, & permissions, ensuring consistent performance across different systems. Containers are like lightweight VMs, making them popular for deploying apps in a standardized way.
What is Docker container? Standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
A read-only definition of a container is image.
Docker Architecture:
+-------------------------------+
| Infrastructure |
+-------------------------------+
| Host Operating System |
+-------------------------------+
| Docker |
+-------------------------------+
| App A | App B | App C | App D | App E | App F |
+-----------------------------------------------+
VM Architecture:
+-------------------------------+
| Infrastructure |
+-------------------------------+
| Hypervisor |
+-------------------------------+
| Virtual Machine | Virtual Machine | Virtual Machine |
|-----------------|-----------------|-----------------|
| Guest OS | Guest OS | Guest OS |
| App A | App B | App C |
+-----------------+-----------------+-----------------+
Is Docker container’s are persistent by default (when we running new ones)? No, but Docker does have ways to support “persistent state” through storage volumes. They’re basically a filesystem that lives outside of the container, but can be accessed by the container.
Cleanup subvolumes, warning this is dangerous!
docker rmi $(docker images --quiet --filter "dangling=true")
docker rmi $(docker images|grep aws) - [ ] not working, need to review
docker system prune after image cleanup.
Docker commands flashcards
- List all Docker containers (running and stopped):
docker ps [-a|--all] - Start a container (run) from an image, with a custom name, publish a container’s port to the host (forwarding):
docker run --name container_name -p hostport:containerport namespace/name:tag,docker run -d -p 8965:80 docker/getting-started:latest. - See the running containers:
docker ps - Start or stop an existing container:
docker start|stop container_name. Stop sendSIGTERMsignal. - Start and pass environment variables when running a Docker container:
docker run -e MY_VAR=my_value my_image - Stop the container by issuing a
SIGKILLsignal to the container:docker kill container_name - Create persistent Docker volume:
docker volume create volume_name - List Docker volumes:
docker volume ls - Inspect the volume to see where it is on your local machine:
docker volume inspect volume_name|jq .[0].Mountpoint - Pull an image from a Docker registry:
docker pull image - Display the list of already downloaded images:
docker images - Open an interactive
ttywith Bourne shell (sh) inside a running container:docker exec [-it|--interactive --tty] container_name sh - Remove stopped containers:
docker rm container1 container2 - Fetch and follow the logs of a container:
docker logs [-f|--follow] container_name - Remove dangling images (isn’t tagged, and isn’t referenced by any container):
docker image prune - A stopped container’s writable layers still take up disk space. To clean this up, you can use following command:
docker container prune - Prune Docker volumes, not used by at least one container (dangerous):
docker volume prune - Docker networks don’t take up much disk space, but they do create
iptablesrules, bridge network devices, and routing table entries. To clean these things up, you can use following command:docker network prune - The ==
docker system prune== command is a shortcut that prunes images, containers, and networks. Volumes aren’t pruned by default, and you must specify the--volumesflag fordocker system pruneto prune volumes. You can limit the scope using the--filterflag, for example:--filter "until=24h"removes items older than 24 hours.