Essential Docker Image Operations

Docker revolutionized application deployment by providing a robust and convenient packaging mechanism. This system encapsulates an application along with its complete runtime environment, including the operating system, ensuring consistent execution across development and production environments. This eliminates the complexities of environmental discrepancies that often lead to "works on my machine" issues.

At the heart of Docker's architecture is the image. An image can be thought of as a static, read-only template, much like a class definition in object-oriented programming. It contains all the necessary components for an application to run. A container, on the other hand, is a live, executable instance of an image, comparable to an object created from a class. While a single image serves as a blueprint, multiple containers can be launched from it, each with its own writable layer for runtime data.

  1. Searching for Images

To discover available images on Docker Hub or other configured registries, use the docker search command. For instance, to find images related to Ubuntu:

docker search ubuntu
  1. Retrieving Images

Downloading an image from a registry to your local Docker daemon is done using docker pull. You can specify an image by its name and an optional tag (e.g., ubuntu:20.04). If no tag is provided, Docker defaults to latest.

docker pull ubuntu:20.04 # Pulls the Ubuntu 20.04 image
docker pull myregistry.example.com/myuser/myimage:1.0 # Pulls from a specific registry

To enhance download speeds, especially from public registries like Docker Hub, configuring a registry mirror is highly recommended. This involves editing the Docker daemon's configuration file and restarting the Docker service.

sudo nano /etc/docker/daemon.json

Add or modify the registry-mirrors entry:

{
  "registry-mirrors": ["https://docker.mirror.aliyuncs.com"]
}

After saving the file, restart the Docker daemon for the changes to take effect:

sudo systemctl restart docker
  1. Listing Local Images

To view all images currently stored on your local machine, use the docker images command. The -a (or --all) flag will display all images, including intermediate layers.

docker images --all
  1. Tagging Images

Tagging allows you to assign additional names or versions to an existing image. This is particularly useful for version control or preparing an image for pushing to a specific repository.

docker tag <IMAGE_ID_OR_NAME> myorganization/custom-app:v1.0

For example, if you have an image with ID a1b2c3d4e5f6 and want to tag it as myorganization/my-ubuntu-base:development:

docker tag a1b2c3d4e5f6 myorganization/my-ubuntu-base:development
  1. Launching a Container from an Image

While strictly a container operation, running a container is the primary way to interact with an image. The docker run command creates and starts a new container based on a specified image.

docker run -itd <IMAGE_NAME_OR_ID>

The flags used here are:

  • -i (--interactive): Keeps STDIN open even if not attached.
  • -t (--tty): Allocates a pseudo-TTY, which allows for an interactive shell.
  • -d (--detach): Runs the container in the background.

For instance, to run a detached interactive Ubuntu container:

docker run -itd ubuntu:latest
  1. Deleting Images

To remove one or more images from your local system, use the docker rmi command. You can specify images by their name/tag or by thier image ID.

docker rmi myorganization/my-ubuntu-base:development

If you specify a tag, only that specific tag is removed from the image. If an image has multiple tags and you remove one, the image file itself will only be deleted when all tags pointing to it are removed, or if you use the image ID to force deletion. When using the image ID, all associated tags are removed, and the image is purged from the system.

docker rmi a1b2c3d4e5f6 # Removes the image by ID, deleting all its tags

Note: An image cannot be removed if its currently being used by a container. You must remove or stop the container first.

  1. Building Custom Images

Custom Docker images are built from a Dockerfile, which is a text file containing a set of instructions. Each instruction creates a new layer in the image.

Here's an example Dockerfile that sets up a basic SSH-enabled Ubuntu environment:

# Base image for our custom build
FROM ubuntu:20.04

# Maintainer information
LABEL maintainer="devops.team@example.com"

# Update package lists, install necessary tools, and set up a user
RUN apt-get update && \
    apt-get install -y openssh-server sudo && \
    rm -rf /var/lib/apt/lists/*

# Create a new non-root user and set password
ARG CUSTOM_USERNAME=appuser
ARG CUSTOM_PASSWORD=securepass

RUN useradd -rm -d /home/${CUSTOM_USERNAME} -s /bin/bash -g root -G sudo ${CUSTOM_USERNAME} && \
    echo "${CUSTOM_USERNAME}:${CUSTOM_PASSWORD}" | chpasswd && \
    mkdir -p /home/${CUSTOM_USERNAME}/.ssh && \
    chown -R ${CUSTOM_USERNAME}:root /home/${CUSTOM_USERNAME} && \
    chmod 700 /home/${CUSTOM_USERNAME}/.ssh

# Set locale
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

# Expose ports for SSH
EXPOSE 22

# Default command to run when the container starts
CMD ["/usr/sbin/sshd", "-D"]

To build an image from this Dockerfile, navigate to the directory containing the file and execute the docker build command. The -t flag assigns a tag to your new image. The . at the end specifies the build context (current directory).

docker build -t myorganization/custom-ssh-ubuntu:v1.0 .
  1. Publishing Images

Once an image is built or modified, you might want to share it with others or store it in a central repository. This process is known as publishing or pushing an image.

Pushing to a Public Registry (e.g., Docker Hub):

First, ensure your image is tagged with the appropriate repository name, which typically includes your Docker Hub username or organization.

docker tag myorganization/custom-ssh-ubuntu:v1.0 yourdockerhubuser/custom-ssh-ubuntu:v1.0

Then, use docker push to upload it. You'll need to be logged in to Docker Hub via docker login.

docker push yourdockerhubuser/custom-ssh-ubuntu:v1.0

Working with a Private Docker Registry:

For private image storage within your network, you can set up a local Docker Registry.

  1. Pull the Registry Image:``` docker pull registry:2
  2. Run the Registry Container: Launch the registry on a specific port (e.g., 5000) and map a host directory for persistent storage of your images. ``` sudo docker run -d -p 5000:5000 --restart=always --name docker-registry -v /opt/docker-registry-data:/var/lib/registry registry:2
  3. Configure Docker Daemon for Insecure Registry (if not using HTTPS): For testing or internal networks, you might need to configure Docker to trust your private registry, especially if it's not using HTTPS. Edit /etc/docker/daemon.json: ``` sudo nano /etc/docker/daemon.json
    
     Add your registry's address (e.g., `192.168.1.100:5000` or `localhost:5000` if on the same host). ```
    {
      "insecure-registries": ["192.168.1.100:5000"]
    }
    
  4. Restart Docker Daemon:``` sudo systemctl restart docker
  5. Tag Your Image for the Private Registry: The tag must include the registry's address. ``` docker tag myorganization/custom-ssh-ubuntu:v1.0 192.168.1.100:5000/custom-ssh-ubuntu:v1.0
  6. Push the Image to Your Private Registry:``` docker push 192.168.1.100:5000/custom-ssh-ubuntu:v1.0
  7. Verify Image in Registry: You can check the catalog of images in your private registry using curl. ``` curl -X GET http://192.168.1.100:5000/v2/_catalog
  8. Pull from Private Registry: To retrieve an image from your private registry: ``` docker pull 192.168.1.100:5000/custom-ssh-ubuntu:v1.0

Tags: docker dockerfile Docker Images containerization Docker Registry

Posted on Thu, 20 Aug 2026 16:43:06 +0000 by vichiq