Docker - Images
Docker - Images
Section titled “Docker - Images”Docker images are the blueprints for containers. They are read-only templates containing the application code, libraries, dependencies, tools, and other files needed for an application to run. Containers are runnable instances of images.
When you run a command like docker run hello-world:
docker: The Docker client command.run: The command to create and start a container.hello-world: The name of the image to use as the template for the container. If this image isn’t found locally, Docker attempts to download (pull) it from a configured registry (Docker Hub by default).
Listing Docker Images (docker images)
Section titled “Listing Docker Images (docker images)”To see the list of Docker images currently stored on your local machine, use the docker images command (or docker image ls).
docker imagesThe output displays information about each image:
REPOSITORY: The name of the image repository (e.g.,ubuntu,nginx,yourusername/myapp).TAG: A specific version or variant identifier within the repository (e.g.,22.04,latest,1.0,stable-alpine). If omitted,latestis often implied.IMAGE ID: A unique identifier for the image.CREATED: How long ago the image was built.SIZE: The amount of disk space the image occupies.
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest fbbc894d6882 7 months ago 13.3kBubuntu 22.04 a7be6190844e 2 weeks ago 77.8MBnginx stable-alpine c7150a2f507e 3 days ago 23.5MBmy-apache-server 1.0 e4b1a9f8d2c1 5 minutes ago 198MBDownloading Images (docker pull)
Section titled “Downloading Images (docker pull)”While docker run automatically pulls an image if it’s not present locally, you can explicitly download an image using docker pull.
# Pull the specific version 8.0 of the MySQL imagedocker pull mysql:8.0This downloads the image layers from the registry (usually Docker Hub) and stores them locally.
Removing Images (docker rmi)
Section titled “Removing Images (docker rmi)”To remove one or more local images and free up disk space, use the docker rmi command (or docker image rm). You can refer to images by their ID, or repository:tag combination.
# Remove the hello-world image by repository and tagdocker rmi hello-world:latest
# Remove the ubuntu image by its IMAGE ID (use the first few unique characters)docker rmi a7be6190844e
# Remove multiple imagesdocker rmi mysql:8.0 my-apache-server:1.0Note: You cannot remove an image that is currently being used by a container (even a stopped one). You must remove the container(s) first (docker rm <container_id>) before removing the image. Use the -f flag (docker rmi -f <image>) to force removal, but use this with caution.
Pruning Images
Section titled “Pruning Images”To remove unused images (dangling images - those without tags, often left over from builds) easily, use:
docker image pruneTo remove all unused images (not just dangling ones):
docker image prune -aInspecting Images (docker inspect)
Section titled “Inspecting Images (docker inspect)”To get detailed, low-level information about an image in JSON format, including its layers, environment variables, exposed ports, and build history, use docker inspect.
docker inspect nginx:stable-alpineThis is useful for understanding how an image was built and configured.
Image Tagging (docker tag)
Section titled “Image Tagging (docker tag)”You can create additional tags that refer to an existing image. This is often used before pushing an image to a registry.
# Tag the local image 'my-apache-server:1.0' with a new name suitable for Docker Hubdocker tag my-apache-server:1.0 yourdockerhubusername/my-web-app:v1Now, yourdockerhubusername/my-web-app:v1 points to the same image ID as my-apache-server:1.0. This doesn’t duplicate the image data.