Run AWS ECR Image via Docker Compose and Environment Variables

Using Docker Compose to run a Docker container with environment variables from an AWS ECR image involves creating a docker-compose.yml file and defining the necessary configurations. Here’s how you can do it:

  1. Install and Configure AWS CLI:
    Ensure you have the AWS CLI installed and configured with the necessary permissions.
  2. Authenticate Docker to the ECR Registry:
    Before you can pull an image from ECR, you need to authenticate your Docker client to the ECR registry you wish to access. Use the aws ecr get-login-password command to get the authentication token:
   aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
  1. Create a docker-compose.yml File:
    Create a docker-compose.yml file in your project directory with the following content:
   version: '3'

   services:
     my-service:
       image: <account-id>.dkr.ecr.<region>.amazonaws.com/<repository-name>:<tag>
       environment:
         VAR_NAME1: value1
         VAR_NAME2: value2

Replace placeholders like <account-id>, <region>, <repository-name>, <tag>, VAR_NAME1, value1, etc., with appropriate values for your setup.

  1. Run Docker Compose:
    Navigate to the directory containing your docker-compose.yml file and run the following command to start the container:
   docker-compose up
  1. (Optional) Verify the Environment Variables:
    If you want to verify that the environment variables were set correctly, you can exec into the running container and print them:
   docker exec -it <container-name_or_id> /bin/sh
   echo $VAR_NAME1
   echo $VAR_NAME2

That’s it! Your Docker container should now be running with the specified environment variables using Docker Compose.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.