Managing Docker Images
The docker images command lists all locally stored Docker images. Key details include:
- REPOSITORY: Name of the image repository
- TAG: Version identifier; defaults to
latestif unspecified - IMAGE ID: Unique identifier for the image
- CREATED: Time elapsed since creation
- SIZE: Disk space used by the image
Common Image Operations
docker images # List local images
docker rmi -f kibana:5.6.9 # Force-remove a specific image
docker images -q # Show only image IDs
docker images --digests # Include digest information
docker images --no-trunc # Display full metadata without truncation
Searching for Images on Docker Hub
Use docker search to find public images:
docker search mysql # Search for MySQL images
docker search -s 30 mysql # Filter results with at least 30 stars
Images tagged as latest typically refer to the most recent stable release, such as MySQL 8.0.
Pulling Images from Remote Registries
The docker pull command downloads an image without running it:
docker pull nginx:latest
Image sizes vary significantly due to base layers. For example, a Tomcat image includes a full OS and JDK, increasing its footprint beyond the standalone application package (typically 50–80 MB).
Running Containers Using docker run
The docker run command creates and starts a container from an image. Common options include:
-i: Keep STDIN open for interaction-t: Allocate a pseudo-TTY-d: Run in detached (background) mode--name: Assign a custom name-p host_port:container_port: Map network ports
Interactive vs Detached Mode
In interactive mode (-it), exiting the shell terminates the container. In detached mode (-d), the container continues running after exit.
docker run -it --name mytomcat -p 8080:8080 tomcat:9.2
Container Lifecycle Management
docker ps # View running containers
docker ps -a # View all containers (including stopped)
docker stop mytomcat # Gracefully stop a container
docker start mytomcat # Restart a stopped container
docker rm mytomcat # Delete a container
docker inspect mytomcat # Retrieve detailed container configuration
Accessing Running Containers
To enter a running container:
docker exec -it mytomcat bash # Enter using bash (if available)
docker exec -it mytomcat sh # Fallback to sh for minimal images
Each container operates in an isolated network namespace with its own IP address. By default, containers use a bridge network where the gateway is often 172.17.0.1, and assigned IPs start from 172.17.0.2.
Creating Custom Images with docker commit
You can create a new image from a modified container using docker commit. This captures changes like added files or configurations.
Example Workflow
- Pull and run a base Tomcat image: ```
docker run -d -p 8081:8080 --name webapp tomcat:8
- Enter the container and add content: ```
docker exec -it webapp bash
cd /usr/local/tomcat/webapps
mkdir mayikt && echo "Hello" > mayikt/index.html
- Create a new image from the modified container: ```
docker commit -m "Added mayikt app" -a "dev-team" webapp custom-tomcat:1.0
- Run a new container from the custom image: ```
docker run -d -p 8088:8080 --name deployed-app custom-tomcat:1.0
Cleaning Up Resources
To remove all containers efficiently:
docker stop $(docker ps -q) && docker rm $(docker ps -aq)
This stops all running containers first, then removes them regardless of state. Repeat if necessary to ensure complete cleanup.