---
# System prepended metadata

title: Query 312

---

Docker clients authenticate to Amazon Elastic Container Registry (ECR) using the AWS Command Line Interface (CLI) or Docker CLI. The authentication process involves creating an authentication token using AWS Identity and Access Management (IAM) credentials and then logging in to the ECR registry using this token.
Here's a description of the authentication process:
1. Generate an authentication token: When you want to interact with an ECR registry, you first need to generate an authentication token. This token is generated by AWS and is used to authenticate your Docker client. The token is generated using your IAM credentials, and it includes an authorization token and an AWS registry URI.
2. Log in to the ECR Registry: After obtaining the authentication token, you can log in to the ECR registry using the Docker CLI. You use the docker login command, providing the ECR registry URI, AWS username (typically `AWS`), and the authentication token as the password. This authenticates your Docker client to pull and push Docker images in the registry.

Here's a C++ code snippet that demonstrates how to generate an ECR authentication token using the AWS SDK for C++ and then use it to log in to an ECR registry:
```
#include <aws/core/Aws.h>
#include <aws/ecr/ECRClient.h>
#include <iostream>

int main() {
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    // Initialize your AWS credentials and region
    Aws::String region = "your-region";
    Aws::Auth::AWSCredentials credentials("your-access-key", "your-secret-key");

    Aws::Client::ClientConfiguration clientConfig;
    clientConfig.region = region;

    Aws::ECR::ECRClient ecrClient(credentials, clientConfig);

    // Define the ECR repository name
    Aws::String repositoryName = "your-ecr-repo-name";

    // Get the ECR authorization token
    Aws::ECR::Model::GetAuthorizationTokenRequest authRequest;
    auto authOutcome = ecrClient.GetAuthorizationToken(authRequest);

    if (authOutcome.IsSuccess()) {
        const Aws::Vector<Aws::ECR::Model::AuthorizationData>& authData = authOutcome.GetResult().GetAuthorizationData();

        if (!authData.empty()) {
            // Extract the authorization token and registry URI
            Aws::String authToken = authData[0].GetAuthorizationToken();
            Aws::String registryUri = authData[0].GetProxyEndpoint();

            // Use the Docker CLI to log in to the ECR registry
            std::string loginCommand = "docker login -u AWS -p " + authToken + " " + registryUri;
            int status = system(loginCommand.c_str());

            if (status == 0) {
                std::cout << "Logged in to ECR registry successfully." << std::endl;
            } else {
                std::cerr << "Failed to log in to ECR registry." << std::endl;
            }
        }
    } else {
        std::cerr << "Failed to get ECR authorization token: " << authOutcome.GetError().GetMessage() << std::endl;
    }

    Aws::ShutdownAPI(options);
    return 0;
}
```
In this code snippet, replace `your-region, your-access-key', 'your-secret-key', and 'your-ecr-repo-name` with your AWS-specific information.
This C++ code uses the AWS SDK for C++ to authenticate a Docker client with an Amazon Elastic Container Registry (ECR). It does so by fetching an ECR authorization token and the registry URI using provided AWS credentials and region. The authorization token and registry URI are then passed to the Docker CLI for login, granting access to pull and push Docker images in the ECR repository. The code ensures proper authentication to the registry and provides feedback on success or failure. This is a simplified example; in practice, you should secure your AWS credentials and integrate this process into your container build and deployment pipelines.