Skip to content
v2.278.0

DockerCredential Class ​

Represents credentials used to access a Docker registry.

Example ​

ts
const dockerHubSecret = secretsmanager.Secret.fromSecretCompleteArn(
  this,
  'DHSecret',
  'arn:aws:...'
);
const customRegSecret = secretsmanager.Secret.fromSecretCompleteArn(
  this,
  'CRSecret',
  'arn:aws:...'
);
const repo1 = ecr.Repository.fromRepositoryArn(
  this,
  'Repo',
  'arn:aws:ecr:eu-west-1:0123456789012:repository/Repo1'
);
const repo2 = ecr.Repository.fromRepositoryArn(
  this,
  'Repo',
  'arn:aws:ecr:eu-west-1:0123456789012:repository/Repo2'
);

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
  dockerCredentials: [
    pipelines.DockerCredential.dockerHub(dockerHubSecret),
    pipelines.DockerCredential.customRegistry(
      'dockerregistry.example.com',
      customRegSecret
    ),
    pipelines.DockerCredential.ecr([repo1, repo2]),
  ],
  synth: new pipelines.ShellStep('Synth', {
    input: pipelines.CodePipelineSource.connection('my-org/my-app', 'main', {
      connectionArn:
        'arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41', // Created using the AWS console * });',
    }),
    commands: ['npm ci', 'npm run build', 'npx cdk synth'],
  }),
});
python
docker_hub_secret = secretsmanager.Secret.from_secret_complete_arn(self, "DHSecret", "arn:aws:...")
custom_reg_secret = secretsmanager.Secret.from_secret_complete_arn(self, "CRSecret", "arn:aws:...")
repo1 = ecr.Repository.from_repository_arn(self, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo1")
repo2 = ecr.Repository.from_repository_arn(self, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo2")

pipeline = pipelines.CodePipeline(self, "Pipeline",
    docker_credentials=[
        pipelines.DockerCredential.docker_hub(docker_hub_secret),
        pipelines.DockerCredential.custom_registry("dockerregistry.example.com", custom_reg_secret),
        pipelines.DockerCredential.ecr([repo1, repo2])
    ],
    synth=pipelines.ShellStep("Synth",
        input=pipelines.CodePipelineSource.connection("my-org/my-app", "main",
            connection_arn="arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41"
        ),
        commands=["npm ci", "npm run build", "npx cdk synth"]
    )
)
java
ISecret dockerHubSecret = Secret.fromSecretCompleteArn(this, "DHSecret", "arn:aws:...");
ISecret customRegSecret = Secret.fromSecretCompleteArn(this, "CRSecret", "arn:aws:...");
IRepository repo1 = Repository.fromRepositoryArn(this, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo1");
IRepository repo2 = Repository.fromRepositoryArn(this, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo2");

CodePipeline pipeline = CodePipeline.Builder.create(this, "Pipeline")
        .dockerCredentials(List.of(DockerCredential.dockerHub(dockerHubSecret), DockerCredential.customRegistry("dockerregistry.example.com", customRegSecret), DockerCredential.ecr(List.of(repo1, repo2))))
        .synth(ShellStep.Builder.create("Synth")
                .input(CodePipelineSource.connection("my-org/my-app", "main", ConnectionSourceOptions.builder()
                        .connectionArn("arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41")
                        .build()))
                .commands(List.of("npm ci", "npm run build", "npx cdk synth"))
                .build())
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
var dockerHubSecret = Secret.FromSecretCompleteArn(this, "DHSecret", "arn:aws:...");
var customRegSecret = Secret.FromSecretCompleteArn(this, "CRSecret", "arn:aws:...");
var repo1 = Repository.FromRepositoryArn(this, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo1");
var repo2 = Repository.FromRepositoryArn(this, "Repo", "arn:aws:ecr:eu-west-1:0123456789012:repository/Repo2");

var pipeline = new CodePipeline(this, "Pipeline", new CodePipelineProps {
    DockerCredentials = new [] { DockerCredential.DockerHub(dockerHubSecret), DockerCredential.CustomRegistry("dockerregistry.example.com", customRegSecret), DockerCredential.Ecr(new [] { repo1, repo2 }) },
    Synth = new ShellStep("Synth", new ShellStepProps {
        Input = CodePipelineSource.Connection("my-org/my-app", "main", new ConnectionSourceOptions {
            ConnectionArn = "arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41"
        }),
        Commands = new [] { "npm ci", "npm run build", "npx cdk synth" }
    })
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
dockerHubSecret := secretsmanager.Secret_FromSecretCompleteArn(this, jsii.String("DHSecret"), jsii.String("arn:aws:..."))
customRegSecret := secretsmanager.Secret_FromSecretCompleteArn(this, jsii.String("CRSecret"), jsii.String("arn:aws:..."))
repo1 := ecr.Repository_FromRepositoryArn(this, jsii.String("Repo"), jsii.String("arn:aws:ecr:eu-west-1:0123456789012:repository/Repo1"))
repo2 := ecr.Repository_FromRepositoryArn(this, jsii.String("Repo"), jsii.String("arn:aws:ecr:eu-west-1:0123456789012:repository/Repo2"))

pipeline := pipelines.NewCodePipeline(this, jsii.String("Pipeline"), &CodePipelineProps{
	DockerCredentials: []DockerCredential{
		pipelines.DockerCredential_DockerHub(dockerHubSecret),
		pipelines.DockerCredential_CustomRegistry(jsii.String("dockerregistry.example.com"), customRegSecret),
		pipelines.DockerCredential_Ecr([]IRepository{
			repo1,
			repo2,
		}),
	},
	Synth: pipelines.NewShellStep(jsii.String("Synth"), &ShellStepProps{
		Input: pipelines.CodePipelineSource_Connection(jsii.String("my-org/my-app"), jsii.String("main"), &ConnectionSourceOptions{
			ConnectionArn: jsii.String("arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41"),
		}),
		Commands: []*string{
			jsii.String("npm ci"),
			jsii.String("npm run build"),
			jsii.String("npx cdk synth"),
		},
	}),
})

Initializer ​

ts
new DockerCredential(usages?: DockerCredentialUsage[])
python
DockerCredential(usages: typing.Optional[typing.List[DockerCredentialUsage]] = None)
java
new DockerCredential(List<DockerCredentialUsage> usages)
csharp
new DockerCredential(DockerCredentialUsage[]? usages = null)
go
NewDockerCredential(Usages *[]DockerCredentialUsage)

Methods ​

customRegistry(registryDomain: string, secret: ISecret, opts?: ExternalDockerCredentialOptions): DockerCredential // chainable@staticmethod def custom_registry(registry_domain: str, secret: ISecret, opts: typing.Optional[ExternalDockerCredentialOptions] = None) -> DockerCredentialpublic static DockerCredential customRegistry(String registryDomain, ISecret secret, ExternalDockerCredentialOptions opts)public static DockerCredential CustomRegistry(string registryDomain, ISecret secret, ExternalDockerCredentialOptions? opts = null)func CustomRegistry(RegistryDomain *string, Secret ISecret, Opts ExternalDockerCredentialOptions) DockerCredential static #
Creates a DockerCredential for a registry, based on its domain name (e.g., 'www.example.com').
dockerHub(secret: ISecret, opts?: ExternalDockerCredentialOptions): DockerCredential // chainable@staticmethod def docker_hub(secret: ISecret, opts: typing.Optional[ExternalDockerCredentialOptions] = None) -> DockerCredentialpublic static DockerCredential dockerHub(ISecret secret, ExternalDockerCredentialOptions opts)public static DockerCredential DockerHub(ISecret secret, ExternalDockerCredentialOptions? opts = null)func DockerHub(Secret ISecret, Opts ExternalDockerCredentialOptions) DockerCredential static #
Creates a DockerCredential for DockerHub.

Convenience method for customRegistry('https://index.docker.io/v1/', opts).

ecr(repositories: IRepository[], opts?: EcrDockerCredentialOptions): DockerCredential // chainable@staticmethod def ecr(repositories: typing.List[IRepository], opts: typing.Optional[EcrDockerCredentialOptions] = None) -> DockerCredentialpublic static DockerCredential ecr(List<IRepository> repositories, EcrDockerCredentialOptions opts)public static DockerCredential Ecr(IRepository[] repositories, EcrDockerCredentialOptions? opts = null)func Ecr(Repositories *[]IRepository, Opts EcrDockerCredentialOptions) DockerCredential static #
Creates a DockerCredential for one or more ECR repositories.
notes

NOTE - All ECR repositories in the same account and region share a domain name (e.g., 0123456789012.dkr.ecr.eu-west-1.amazonaws.com), and can only have one associated set of credentials (and DockerCredential). Attempting to associate one set of credentials with one ECR repo and another with another ECR repo in the same account and region will result in failures when using these credentials in the pipeline.

grantRead(grantee: IGrantable, usage: DockerCredentialUsage): voiddef grant_read(self, grantee: IGrantable, usage: DockerCredentialUsage) -> Nonepublic void grantRead(IGrantable grantee, DockerCredentialUsage usage)public void GrantRead(IGrantable grantee, DockerCredentialUsage usage)func GrantRead(Grantee IGrantable, Usage DockerCredentialUsage) #
Grant read-only access to the registry credentials.

This grants read access to any secrets, and pull access to any repositories.