Double-click on the image to zoom-out ...Larger.
To return to Home page: Refresh Page or Take ESC Button on Keyboard
Key Concepts
Docker image Commands
Docker provides several commands to manage images effectively. Here are the most commonly used Docker image commands:
1. Building Images
docker build -t <image_name>:<tag> .
- Builds an image from a Dockerfile in the current directory (
.
). - Example:
- Builds an image from a Dockerfile in the current directory (
2. Listing Images
docker images
- Displays a list of locally available images.
- Example output:
3. Removing Images
docker rmi <image_id>
- Deletes a specific image by its ID or name.
- Example:
docker rmi $(docker images -q)
- Removes all images (use cautiously).
4. Tagging Images
docker tag <source_image> <new_image>:<tag>
- Assigns a new name to an existing image.
- Example:
5. Pushing & Pulling Images
docker push <image>:<tag>
- Uploads an image to a registry (e.g., Docker Hub).
- Example:
docker pull <image>:<tag>
- Downloads an image from a registry.
- Example:
6. Inspecting Images
docker inspect <image>
- Displays detailed metadata of an image.
- Example:
7. Saving & Loading Images
docker save -o <file>.tar <image>
- Saves an image as a
.tar
file. - Example:
- Saves an image as a
docker load -i <file>.tar
- Loads an image from a
.tar
file. - Example:
- Loads an image from a
8. Pruning Unused Images
docker image prune -a
- Removes unused and dangling images to free space.
- Example:
Docker is a powerful containerization platform that simplifies application deployment. Here are the key concepts:
1. Containers
- A container is a lightweight, standalone, and executable package that includes everything needed to run an application: code, runtime, system tools, libraries, and dependencies.
- Containers isolate applications from the host system, making them portable and consistent across different environments.
Example:
This runs an Nginx container in detached mode and maps port 8080 (host) → 80 (container).
2. Images
- A Docker image is a blueprint for containers. It includes the application code, dependencies, and environment settings.
- Images are immutable (unchangeable after creation).
- Images are stored in a registry like Docker Hub or private repositories.
Example:
This downloads the latest devopspatemf2021/twtech:twtech-spring-boot-mongo image.
3. Dockerfile
- A Dockerfile is a script with instructions to build an image.
Example Dockerfile:
To build an image from this file:
4. Volumes (Persistent Storage)
- Volumes allow containers to persist data beyond their lifecycle.
- Stored in
/var/lib/docker/volumes/
.
Example:
5. Networking
Docker provides networking to allow communication between containers and external systems.
Network Type | Description |
---|---|
Bridge (default) | Isolated network for containers on the same host. |
Host | Uses the host's networking directly. |
Overlay | Used in Swarm mode for multi-host networking. |
None | No networking, completely isolated. |
Example:
6. Container Lifecycle
Command | Description |
---|---|
docker run | Creates and starts a container. |
docker start | Starts an existing container. |
docker stop | Stops a running container. |
docker restart | Restarts a container. |
docker rm | Removes a container. |
Example:
7. Registries
Docker images are stored in registries, either public or private.
Registry | Description |
---|---|
Docker Hub | The default public registry. |
AWS ECR, Azure ACR, Google GCR | Cloud-based private registries. |
8. Docker Swarm & Kubernetes
- Docker Swarm: Native clustering for managing multiple containers.
- Kubernetes: A powerful orchestration tool for managing large-scale containerized applications.
9. Security Best Practices
Use official and verified images.
Avoid running containers as root (USER node
).
Use network segmentation (docker network
).
Scan for vulnerabilities (docker scan myapp
).
Keep Docker updated.
twtech-Thoughts:
Images = Blueprints for containersContainers = Running instances of images
Dockerfile = Automates(instructs) image creation
Docker Compose = Manages multi-container apps
Volumes = Persistent data storage
Networking = Container communication
Docker Commands to access containers.
To access and interact with running Docker containers, use the following commands:
1. List Running Containersdocker ps
- Shows active containers.
- Example output:
docker ps -a
- Lists all containers, including stopped ones.
docker exec -it <container_name_or_id> <command>
- Runs a command inside a container.
- Example (start an interactive Bash shell in a container):
- If the container has only
sh
:
docker attach <container_name_or_id>
- Connects your terminal directly to a running container’s output.
- Example:
- Press
Ctrl + P, Ctrl + Q
to detach without stopping the container.
docker start -ai <container_name_or_id>
- Starts a stopped container and attaches to it.
- Example:
- Copy files from host to container:
- Copy files from container to host:
docker inspect <container_name_or_id>
- Shows detailed information about a container, including IP address.
- Example:
docker logs <container_name_or_id>
- Displays logs from a container.
- Example:
- To follow logs in real time:
docker top <container_name_or_id>
- Lists active processes inside a container.
- Example:
- For Bash:
- For SH (if Bash is not available):
Check list
Here’s a Docker Checklist to ensure best practices while working with Docker:
Basic Setup & Installation Install Docker Engine
Verify installation using:
Add your user to the Docker group (optional for non-root access):
Dockerfile Best PracticesStart with a minimal base image (alpine
, ubuntu
, etc.)
Use multi-stage builds to reduce image size
Always specify a fixed image version when applicable(e.g., nginx:1.21.3
, not nginx:latest
)
Minimize the number of layers by chaining commands:
Use COPY
instead of ADD
unless extracting tar files
Set a non-root user for security:
Container Management
List running containers:
Remove unused containers:
Remove all stopped containers:
Image Management
Build an image:
List local images:
Remove an image:
Remove unused images:
Volume & Network Management
List volumes:
Remove unused volumes:
List networks:
Create a network:
Connect a container to a network:
Docker Compose Checklist
docker-compose.yml
to define multi-container appsStart services in detached mode:
Stop and remove containers:
Scale a service:
Security Best Practices
Use non-root users inside containers
Limit container capabilities using security options
Scan images for vulnerabilities:
Avoid exposing unnecessary ports (EXPOSE
only required ports)
View container logs:
View container resource usage:
Run a shell inside a running container:
Check detailed container info:
Cleanup & Maintenance
Remove all unused containers, images, and volumes:
Monitor disk usage:
Enable Docker if needed:
Restart Docker if needed:
No comments:
Post a Comment