This lesson explores best practices for securing container images, including image naming, secure repositories, and configuring pods for private registries.
In this lesson, we explore best practices for securing container images. We start with the fundamentals of image naming, then move to using secure image repositories, and finally discuss how to configure your pods to pull images from private registries.Throughout this lesson, you will find practical examples that deploy various applications, including web apps, databases, and Redis caches. Let’s begin by examining a simple pod definition file.For instance, consider the following pod configuration:
In this example, the configuration uses the Nginx image to deploy a container. But what exactly is this image, and where does it come from?When you specify an image like “nginx”, you are following Docker’s image naming convention. In this case, “nginx” refers to “library/nginx” because, without a user or account name, Docker defaults to the “library” account. This account hosts Docker’s official images, maintained by a dedicated team to ensure best practices.If you create your own account and publish images, you follow the same naming pattern. For example, your image might be stored under “yourname/imagename” instead of “library/imagename”.Since our configuration does not specify a location, the image is pulled from Docker’s default registry, Docker Hub, accessible via the DNS name docker.io. In practice, when you reference the image as:
Copy
Ask AI
image: library/nginx
It is actually stored and pulled from:
Copy
Ask AI
image: docker.io/library/nginx
Whenever you create or update an image, you push it to the registry, and each time your application is deployed, the image is pulled from that registry.
Docker Hub: The default registry where official and community images are stored.
Google Container Registry (GCR): Located at gcr.io, it hosts many Kubernetes-related images, including those used for end-to-end cluster testing. These images are publicly accessible.
Private Registries: For in-house applications that should remain confidential, it is best to host your images in a private registry. Many cloud service providers, including AWS, Azure, and GCP, offer private registries by default.
Restrict access to your repository using credentials, regardless of whether you’re using Docker Hub, GCR, or an internal solution.
To run a container from a private image, you must log in to the private registry with the Docker CLI. For example:
Copy
Ask AI
docker login private-registry.ioLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: registry-userPassword:WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.Login Succeeded
After logging in, you can run your application using the private image:
In Kubernetes, you need to provide the necessary credentials to allow pods to pull images from a private registry. This is achieved by creating a secret object that stores your Docker registry credentials.First, create the secret using the following command:
When the pod is created, Kubernetes uses the credentials stored in the secret to authenticate with the private registry and pull the required image.
Ensure that your secret’s name in the pod definition matches the name you provided when creating the secret.
This concludes our lesson on image security. By following these steps, you can securely deploy container images from both public and private registries in your Kubernetes clusters.For further reading, consider checking out: