During these days, even if you have seen not that much from the social point of view, I have continued working on Docker and I am thinking about something you could enjoy in the future!
In the meanwhile, here is a Thread of useful Docker Tips
👇👇👇
DOCKER DESKTOP
Dockerfile keywords
DOCKER EXEC
Execute commands on currently running containers.
The most popular usage is to launch a Terminal in a running container, keeping the STDIN open and allocating a pseudo-TTY.
Example:
"docker exec -it <cont_ID> /bin/bash"
HEALTHCHECK in Dockerfiles
Healthchecks are what they sound like: a way to check if the container is 'healthy'. You can specify an healthcheck to check if a container is running properly.
They are very important in a real-world scenario and production-ready applications
If a service is defined in both files, configurations are merged, useful for development
Docker-compose commands
docker stats [OPTIONS] [CONTAINER...]
returns a live data stream for containers.
Below a custom command, using GO template, that shows additional info you can try.
docker stats -a --format "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.BlockIO}}"
DELETE ALL NOT RUNNING CONTAINERS
CHECK ENVIRONMENT VARIABLES OF AN IMAGE
DOCKER TOP: Displays the container's running processes
DOCKER PORT: Lists container's port mappings
DOCKER KILL: Kills the process! (not ideal)
DOCKER DIFF
An handy command to check any changes to:
-files
-directories
on the container’s filesystem
A: added file or directory
C: changed file or directory was
D: deleted file or directory
Usage:
docker diff <container_ID/container_name>
docker container ps [OPTIONS]
(you can use the shortcut: "docker ps [OPTIONS]")
-a: show all containers, even the stopped ones
-q: only show IDS
-l: show the latest container
-s: show the size
-f: filter output
--format: show using GO template (example below)
DOCKER TAG:
docker tag <image> <new tag>
Effect:
Create a tag image that refers to the source image
A tag must be:
- valid ASCII
- may contain letters, digits, underscores, periods, dashes.
- may not start with a period or a dash
- maximum of 128 characters.
DOCKER VOLUMES:
The preferred mechanism for persisting data for Docker containers
- Special directory for containers
- Designed to persist data
- No automatically deleted
- No garbage-collected when no more referenced
Types:
-HOST VOLUMES
-NAMED VOLUMES
-ANONYMOUS VOLUMES
DOCKER UN OPTIONS:
-d: detached mode
-e: set/override env variable
-i: Keep STDIN open
--name: assign name
--network: define network
-p: publish ports
-P: publish all the ports to the host interfaces
--rm: remove on exit
-t: pseudo-tty
-u: user
-v: volume
-w: default workdir
Containers pro over VMs:
1⃣Dev-Friendly
2⃣Smaller Snapshots (Images)
3⃣Public Registry (Docker Hub)
4⃣Faster startup (seconds, no minutes)
5⃣Less Resource-Consuming (Processes)
6⃣The App runs independently on computer/OS
7⃣New containers can be created on-demand quickly
.dockerignore file
DOCKER PRUNE COMMANDS
DOCKER STOP / DOCKER RM deep dive
What happens when we use these commands?
How can we send a SIGNAL to a container?
Docker Volume PRO over Bind-Mount:
1⃣Easier back up/migration
2⃣Manageable by Docker CLI/API
3⃣Work on Linux/Windows containers
4⃣Shared safer among containers
5⃣Storable on remote hosts/cloud providers to encrypt contents/add functionalities
6⃣Can have content pre-populated
DOCKER NAMESPACES
Docker is written in Go and takes advantage of several features of the Linux kernel to deliver its functionality.
One of these is 'Namespaces':
The Docker Engine combines
- namespaces
- cgroups
- UnionFS