How to run, test or debug a Docker image locally from AWS ECR

You’ve created a new Docker container and pushed it to ECR. The container is then deployed via ECS to your environment, but you see a ‘502 Bad Gateway’ error in your browser. One reason could be the container. Here are a few commands to run and test your container locally to debug the issue.

Before you can download and run the Docker image you need to log in to ECR. To retrieve the ECR login token use this AWS CLI command.

aws ecr get-login --region eu-west-1 --no-include-email --profile [profile-name]

This returns the docker login command with the token. Copy the output and execute the command to log in to the registry.

Now you can use the Docker CLI to download and run an image from ECR.

docker run -d -p 8080:80 [aws_account_id].dkr.ecr.[region].amazonaws.com/[docker-image-name]:[tag]

You can now try localhost:8080 to view the image in your browser.

To SSH into a running container you can use the following commands.

List running Docker images:
docker ps

From the list note the image name and use this command to SSH into the container.
docker exec -it [image-name] /bin/bash

Return low level information on the Docker image:
docker inspect [image-id]