IBM Cloud® Container Registry is a platform designed for storing and managing the container images associated with your applications. This service offers a secure, scalable, and highly available private registry with encryption, accommodating multiple users within your IBM Cloud Account.
The Container Registry Python SDK offers various functions, from creating namespaces to tagging/listing images, inspecting and deleting images. It also allows you to retrieve plan and quota information for the account. Additionally you can manage retention policy for the images and restore deleted images.
This article delves into the process of leveraging the Python SDK to create and delete namespaces within the IBM Cloud® Container Registry (ICR).
Prerequisites:
Before diving into the tutorial, ensure you have the following prerequisites in place:
- IBM Cloud account
- Appropriate IAM permissions for IBM Cloud Container Registry
- Python 3.7 or above installed on your system
- You can refer to the Python SDK here
Step 1: Installing container-registry package
Begin by installing the Container-registry package using either pip or easy_install:
pip install --upgrade "ibm-container-registry>=1.1.0"
# or
easy_install --upgrade "ibm-container-registry>=1.1.0"
Step 2: Creating IBM Cloud API Key
Generate a new IBM Cloud API key from this link


Step 3: Define environment variables
Create a .env file named container_registry.env and include the following contents:
CONTAINER_REGISTRY_URL=https://us.icr.io
CONTAINER_REGISTRY_AUTH_TYPE=iam
CONTAINER_REGISTRY_APIKEY=IBMCLOUD_APIKEY
Note: Replace the registry URL with your desired ICR region, you can review available regions here. Set IBMCLOUD_APIKEY with your IBM Cloud API Key created in Step 2. Ensure no quotes are used in defining these values in the above .env file.
Step 4: Creating namespace using Python SDK
In this example, we'll utilise snippets from the Python SDK documentation to illustrate namespace creation and deletion. Modify the code based on your specific requirements.
Create the namespace.py with the below code, make sure container_registry.env created in Step 3 and this file are placed in the same directory.
import os
import pytest
from ibm_cloud_sdk_core import ApiException, read_external_sources
from ibm_container_registry.container_registry_v1 import *
account = 'ACCOUNT-ID' # Replace this with your AccountId
config_file = 'container_registry.env'
container_registry_service = None
config = None
namespace_link = None
class TestContainerRegistryV1Examples():
"""
Example Test Class for ContainerRegistryV1
"""
@classmethod
def setup_class(cls):
global container_registry_service
if os.path.exists(config_file):
os.environ['IBM_CREDENTIALS_FILE'] = config_file
# begin-common
container_registry_service = ContainerRegistryV1.new_instance(account=account)
# end-common
print(container_registry_service)
assert container_registry_service is not None
# Load the configuration
global config
config = read_external_sources(ContainerRegistryV1.DEFAULT_SERVICE_NAME)
print('Setup complete.')
needscredentials = pytest.mark.skipif(
True, reason="Examples for this SDK are not intended to be runnable, skipping..."
)
@needscredentials
def test_create_namespace_example(self):
"""
create_namespace request example
"""
try:
# begin-create_namespace
namespace = container_registry_service.create_namespace(
name=input("Enter the namespace\n")
).get_result()
print(json.dumps(namespace, indent=2))
# end-create_namespace
global namespace_link
namespace_link = namespace['namespace']
except ApiException as e:
pytest.fail(str(e))
@needscredentials
def test_list_namespaces_example(self):
"""
list_namespaces request example
"""
try:
# begin-list_namespaces
print("Listing the namespaces")
result = container_registry_service.list_namespaces().get_result()
print(json.dumps(result, indent=2))
# end-list_namespaces
except ApiException as e:
pytest.fail(str(e))
myObject = TestContainerRegistryV1Examples()
myObject.setup_class()
myObject.test_create_namespace_example()
myObject.test_list_namespaces_example()
Note: Replace the value of ACCOUNT-ID with the corresponding value from this link.
Execute the code mentioned above. A prompt will appear for you to enter the namespace. I've set up the namespace as "test-madhu-namespace."

You can verify if the namespace has been created in the IBM Cloud Container Registry. Given that the region is us.icr.io, corresponding to Dallas, you can see the namespace has been created.

Step 5: Deleting the namespace
In the same codebase provided in Step 4, you can add the below function call,
@needscredentials
def test_delete_namespace_example(self):
"""
delete_namespace request example
"""
try:
# begin-delete_namespace
response = container_registry_service.delete_namespace(
name=input("Enter the namespace to delete\n")
).get_result()
print(json.dumps(response, indent=2))
# end-delete_namespace
except ApiException as e:
pytest.fail(str(e))
myObject = TestContainerRegistryV1Examples()
myObject.setup_class()
myObject.test_delete_namespace_example()
I've created a new .py file to isolate the deletion code. Run the delete-ns.py file, and a prompt will request the namespace you want to remove. Let's proceed with deleting the previously created test-madhu-namespace.

From IBM Cloud Container Registry you can verify the namespace has been deleted.

By following these steps, you can seamlessly work with IBM Cloud Container Registry namespaces using the Python SDK. Thank you!