Accessing container registries
Images that are used by IBM Cloud® Code Engine are typically stored in a registry that can either be accessible by the public (public registry) or set up with limited access for a small group of users (private registry).
A container registry, or registry, is a service that stores container images. For example, IBM Cloud Container Registry and Docker Hub are container registries. A container registry can be public or private. A container registry that is public does not require credentials to access. In contrast, accessing a private registry does require credentials.
Code Engine requires access to container registries to complete the following actions:
- To retrieve (or "pull") a container image to run an app or job
- To store a newly created container image as an output of an image build
- To store and retrieve local files when a build is run from local source
Code Engine handles many of the underlying details of the interactions between the system and your registry.
To pull images from a registry, Code Engine uses a special type of Kubernetes secret that is called an imagePullSecret
. This image pull secret stores the credentials to access a container registry. When you add access to a container
registry with Code Engine to pull images, you are creating an image pull secret. For more information about image pull secrets, see Kubernetes documentation.
Types of image registries
Images are typically stored in a registry that can either be accessible by the public (public registry) or set up with limited access for a small group of users (private registry).
Public registries, such as public Docker Hub, can be used to get started with Docker and Code Engine to create your first application or job. But when it comes to enterprise workloads, use a private registry, such as the one provided in IBM Cloud Container Registry to protect your images from being used by unauthorized users. For private registries, use registry secrets to ensure that the credentials are available to gain access to the private registry.
Registry | Description |
---|---|
IBM Cloud Container Registry |
With this type of registry, you can set up your own secured image repository in IBM Cloud Container Registry where you can safely store and share images between users.
|
Any other private registry |
Connect any existing private registry to Code Engine by adding access. Adding access securely saves your registry URL and credentials in a Kubernetes secret.
|
Public Docker Hub |
Use this type of registry to pull existing public images from Docker Hub directly in your Code Engine applications or jobs. Important:
With public Docker Hub, you can:
|
Types of registry secrets
To access images in a registry, Code Engine uses one of the following types of registry secrets.
- Code Engine managed secret - If your registry uses an IBM Cloud Container Registry namespace that is in your account, then you can let Code Engine create and manage the registry secret for you. In the console, this automatically created registry
secret is called a
Code Engine managed secret
. In the CLI, the name of an automatically created registry secret is of the format,ce-auto-icr-private-<region>
. - User managed secret - This is a secret that you create and manage. You can access images from your account with an API key or use an access token for the container registry of your choice; for example, Docker Hub. For this case, the registry secret that is listed in the console is the name of your registry secret.
If your registry is public and does not require credentials; for example, Code Engine sample images in icr.io/codeengine
or Docker Hub public, then you do not need a registry secret. For this case, the registry secret that is listed
in the console is None
.
Accessing images from a public account
If your image is stored in a public repository, such as a public Docker Hub, then you can simply reference the image directly when you deploy your application or run your job. Note that while storing images in a public registry is fine for getting started with applications and jobs, store your enterprise images in a private registry.
Accessing images in your own account from console
If you are accessing Code Engine from an account that you own or administer, then Code Engine can automatically push and pull images to and from an IBM Cloud Container Registry namespace in your account when you create or update apps, jobs, or builds from the console. Code Engine can even create a namespace for you when you push an image. For more information, see the following topics.
Accessing images from your account with an API key
If you are accessing Code Engine with the CLI, you must first create an IAM API key and then save the IAM API key as registry access in Code Engine.
The following steps create an API key that stores the credentials of a user ID. Instead of using a user ID, you might want to create an API key for a service ID that has an IBM Cloud IAM service access policy to IBM Cloud Container Registry. If you choose to authenticate a user ID, make sure that the user is a functional ID or plan for cases where the user leaves so that Code Engine can still access the registry.
Creating an API key from the console
To create an IBM Cloud IAM API key from the console,
-
Launch Access (IAM) Overview.
-
Select API keys.
-
Click Create an IBM Cloud API key.
-
Enter a name and optional description for your API key and click Create.
-
Copy the API key or click download to save it.
You won’t be able to see this API key again, so be sure to record it in a safe place.
Now that you created your API key, save it as registry access.
Creating an API key with the CLI
To create an IBM Cloud IAM API key with the CLI, run the iam api-key-create
command. For example, to create an API key
called cliapikey
with a description of My CLI API key
and save it to a file called key_file
, run the following command:
ibmcloud iam api-key-create cliapikey -d "My CLI API key" --file key_file
If you choose to not save your key to a file, you must record the API key that is displayed when you create it. You cannot retrieve it later.
Now that you created your API key, save it as registry access.
Accessing images in a different account
You can assign IBM Cloud IAM access policies to users or a service ID to restrict permissions to specific registry image namespaces or actions (such as push or pull). Then, create an API key and store these registry credentials in Code Engine.
For example, to access images in other IBM Cloud accounts, create an API key that stores the IBM Cloud Container Registry credentials of a user or service ID in that account. Then, in Code Engine, use that key to create access in your account.
Accessing images in a private Docker Hub account
To access images in a private Docker Hub account, create registry access by providing your password or an access token. By using an access token, you can more easily grant and revoke access to your Docker Hub account without requiring a password change. For more information about access tokens and Docker Hub, see Managing access tokens.
After you decide whether to use your password directly or to create an access token, create your registry access.
Add registry access to Code Engine
To set up access to an IBM Cloud Container Registry in a different IBM Cloud account, to pull images from a private Docker Hub account, or to pull or push images by using the Code Engine CLI, you can use the IBM API key or Docker Hub password or access token to create registry access through Code Engine to store your authentication key or token for you.
Adding registry access from the console
Before you begin, create a project.
- After your project is in Active status, click the name of your project on the Code Engine Projects page.
- From the Components page, click Secrets and configmaps.
- From the Secrets and configmaps page, click Create to create your secret.
- From the Create secret or configmap page, complete the following steps:
- Select Registry secret, and click Next.
- Provide a name; for example,
mysecret-registry
. - Specify the target registry for this secret, such as IBM Cloud Container Registry or Docker Hub.
- Specify the location of the registry.
- Specify a username. If this secret is for IBM Cloud Container Registry, the username is
iamapikey
. If this secret is for Docker Hub, it is your Docker ID. - Enter the credentials for the username. For IBM Cloud Container Registry, use your IAM API key. For Docker Hub, you can use your Docker Hub password or an access token. For other target registries, specify the password or API key for the username.
- Click Create to create the secret.
Now that your secret is created from the console, go to the Secrets and configmaps page to view a list of defined secrets and configmaps. You can apply filters to customize the list to meet your needs.
You can add access to a container registry when you create an application or job, or when you build an image. Click Configure image and specify the container image to run, including the registry where the image is stored and the registry access to use to retrieve the image.
Adding registry access with the CLI
Beginning with CLI version 1.42.0, defining and working with secrets in the CLI is unified under the secret
command group. See ibmcloud ce secret
commands. Use the --format
option to specify the category of secret, such as basic_auth
, generic
, ssh
, tls
, or registry
. While you can continue to use the registry
command group, take advantage of the unified secret
command group. To create a secret to access a container registry, use the ibmcloud ce secret create --format registry
command. To learn more about working with secrets in Code Engine, see Working with secrets.
To add IBM Cloud Container Registry or Docker Hub access with the CLI, use the secret create --format registry
command. This command requires a name of the registry secret, the URL of the registry server, and
the username and password information to access the registry server, and also allows other optional arguments. For a complete listing of options, see the ibmcloud ce secret create
command.
For example, the following command creates registry access to an IBM Cloud Container Registry instance called myregistry
that is on the us.icr.io
registry server:
ibmcloud ce secret create --format registry --name myregistry --server us.icr.io --username iamapikey --password API_KEY
Example output
Creating registry secret 'myregistry'...
OK
The following table summarizes the options that are used with the secret create --format registry
command in this example. For more information about the command and its options, see the ibmcloud ce secret create
command.
Option | Description |
---|---|
--name |
The name of the registry secret. Use a name that is unique within the project. This value is required.
|
--server |
Enter the URL of the registry server. For Container Registry, the server name is <region>.icr.io . For example, us.icr.io . For Docker Hub, the value is https://index.docker.io/v1/ . |
--username |
Enter the username to access the registry server. For Container Registry, it is iamapikey . For Docker Hub, it is your Docker ID. |
--password |
Enter the password. For Container Registry, the password is your API key. For Docker Hub, you can use your Docker Hub password or an access token. |
Controlling access to Container Registry for Code Engine workloads
Suppose that you want to control access to IBM Cloud Container Registry when Code Engine pulls images. For example, you want to control access to Container Registry to specific IP addresses. Consider the following approaches.
-
Use a context-based restriction. By using a context-based restriction, if the IP addresses for your Code Engine project ever change, you do not need to change your access. You can restrict access to Container Registry to a network zone, where your network zone includes Code Engine and anything that requires access to the registry.
-
Disable the public access to IBM Cloud Container Registry and make sure Code Engine uses the private endpoints instead of the public endpoints. See Securing your connection to Container Registry.
-
To control access by a specific IP range, use an API endpoint to fetch the IP addresses for your particular Code Engine project. It is important to note that these IP addresses are subject to change, and you must take appropriate steps when this happens. See Code Engine public and private IP addresses and How can I add my Code Engine app to an allowlist?
Considerations for images in your registry
The name of your image that is used for your app or job must be in one of the following formats.
REGISTRY/NAMESPACEorDOCKERUSERorDOCKERORG/REPOSITORY:TAG
whereREGISTRY
andTAG
are optional. IfREGISTRY
is not specified, the default isdocker.io
. IfTAG
is not specified, do not include the colon (:). The default forTAG
islatest
.REGISTRY/NAMESPACEorDOCKERUSERorDOCKERORG/REPOSITORY@IMAGEID
whereREGISTRY
is optional. IfREGISTRY
is not specified, the default isdocker.io
andibm
as the Docker organization.
Component | Characters allowed | Length | Additional rules |
---|---|---|---|
REGISTRY |
a-zA-Z0-9 -_. --__ |
1-253 | (0-127Periods)(label:1-63,noDashOnEnd) |
NAMESPACE |
a-z 0-9 -_ --__ |
4-30 | (start/end with letterOrNumber) |
DOCKERUSERorDOCKERORG |
a-z 0-9 |
4-30 | |
REPOSITORY |
a-z 0-9 -_. / |
2-255 | (start/end with letterOrNumber) |
TAG |
a-zA-Z0-9 -_. --__.. |
0-128 | (NOT start with periodOrDash) |
IMAGEID |
a-z 0-9 : |
(startwith sha256: noOtherColon) |
The parts of the image name must meet the following criteria.
REGISTRY
must be 253 characters or fewer and can contain lowercase or uppercase letters, numbers, periods (.), hyphens (-), and underscores (_
). Do not use a dash (.) as the last character. Do not use more than 127 periods (.) and the labels between them can be between 1 and 63 characters long.NAMESPACE
must be between 4 and 30 characters and must begin and end with a lowercase letter or number.NAMESPACE
can contain lowercase alphanumeric characters, hyphens (-), and underscores (_
).DOCKERUSERorDOCKERORG
can be used for Docker registries instead ofNAMESPACE
. Specify your Docker username or Docker organization. Your Docker username and organization must be between 4 and 30 characters and contains only lowercase alphanumeric characters or numbers.REPOSITORY
must be between 2 and 255 characters and must begin and end with a lowercase letter or number.REPOSITORY
can contain lowercase alphanumeric characters, forward slashes (/), periods (.), hyphens (-), and underscores (_
).TAG
must be between 0 and 128 characters and can contain lowercase or uppercase letters, numbers, periods (.), hyphens (-), and underscores (_
). TheTAG
must not begin with a period or dash. If you do not include aTAG
, do not include the colon either.IMAGEID
is prefixed withsha256:
and can contain lowercase letters and numbers.