# DockerCredential

Represents credentials used to access a Docker registry.

Obtainable from: DockerCredential.customRegistry, DockerCredential.dockerHub, DockerCredential.ecr

- Source: [`pipelines/lib/docker-credentials.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/pipelines/lib/docker-credentials.ts#L11)

## Example

TypeScript:

```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:

```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:

```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();
```

C# / .NET:

```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" }
    })
});
```

Golang:

```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

TypeScript:

```ts
new DockerCredential(usages?: DockerCredentialUsage[])
```

Python:

```python
DockerCredential(usages: typing.Optional[typing.List[DockerCredentialUsage]] = None)
```

Java:

```java
new DockerCredential(List<DockerCredentialUsage> usages)
```

C# / .NET:

```csharp
new DockerCredential(DockerCredentialUsage[]? usages = null)
```

Golang:

```go
NewDockerCredential(Usages *[]DockerCredentialUsage)
```

## Methods

- `customRegistry(registryDomain: string, secret: ISecret, opts?: ExternalDockerCredentialOptions): DockerCredential // chainable` (static) — Creates a DockerCredential for a registry, based on its domain name (e.g., 'www.example.com').

- `dockerHub(secret: ISecret, opts?: ExternalDockerCredentialOptions): DockerCredential // chainable` (static) — Creates a DockerCredential for DockerHub.

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

- `ecr(repositories: IRepository[], opts?: EcrDockerCredentialOptions): DockerCredential // chainable` (static) — Creates a DockerCredential for one or more ECR repositories.

  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): void` — Grant read-only access to the registry credentials.

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