Skip to content

Instantly share code, notes, and snippets.

@usama2490
Last active June 9, 2022 07:34
Show Gist options
  • Save usama2490/b28563d0e6248759ba730ee36c14d5c3 to your computer and use it in GitHub Desktop.
Save usama2490/b28563d0e6248759ba730ee36c14d5c3 to your computer and use it in GitHub Desktop.
This gist will guide you how to setup the ECR registry authentication with Docker and pulling of ECR images defined in Docker Compose file.

Install the awscli v2 first

Run these commands to have it installed,

unzip awscliv2.zip
cd aws
./install

Run aws --version cmd, it should return 2.x.x version (of aws cli)

Configure AWS credentials

Configure your AWS creds with ECR Registry access with "aws configure" OR put them directly by editing the ~/.aws/credentials file with the right region defined.

Note: Ask your manager or admin for the AWS credentials if you don't have.

Authenticate Docker with ECR

Authenticate Docker with AWS ECR in order to pull images into the system (wher Docker is running): Run this cmd without sudo first, if didn't work try adding sudo at the start of this command.

$ aws ecr get-login-password --region <ecr_region> | sudo docker login --username AWS --password-stdin <account_id>.dkr.ecr.<ecr_region>.amazonaws.com

You will see the Login Succeed message in the console if the command ran successfully

Note: awscli version 2 is required to run this cmd sussessfully, else it'd fail. You'll need to do this step every single time when the session get expire, i.e. When Docker is unable to pull the images from ECR (Pull access denied error)

Edit docker-compose manually OR autometically with compose_generator

Edit the main docker-compose.yml file with the image URI like.

....
dashboard:
    image: "${ECR_DOCKER_REGISTRY_URI}/dashboard"
    container_name: dashboard
    volumes:
      - tmpfs:/usr/share/nginx/html/tmpfs
      - ${CERT_DIR}:${CERT_DIR}
      ....

Note: If you have the compose_generator script then it will autometically add the ECR image path for the services which will be enabled by user in runtime.

Run compose with latest ECR images

Follow these steps to Run docker-compose with latest ECR images

Step into the docker-compose.yml directory and run the following commands,

Stop containers if there are any running already
$ sudo docker-compose stop

Remove containers along with their images in order to avoid any conflict or confusion
$ sudo docker-compose rm -f

Pull the latest ECR images defined in yaml file
$ sudo docker-compose pull

Run the containers in detach mode
$ sudo docker-compose up -d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment