Search code examples
amazon-web-servicesdockeramazon-elastic-beanstalk

How to create AWS Elastic Beanstalk instance using docker container in a private AWS container registry?


I pushed a docker container to AWS container registry and am wondering how to use it to create an AWS elastic beanstalk instance? When manually creating an eb instance through the console, it says I can upload a file, but not sure how to use the file in the container registry, either through the console or with the aws cli or eb cli?


Solution

  • To use a Docker container that you've pushed to the AWS Elastic Container Registry (ECR) to create an AWS Elastic Beanstalk (EB) environment, you need to create a Dockerrun.aws.json file. This file specifies how AWS Elastic Beanstalk should deploy the Docker container.

    So start by creating a Dockerrun.aws.json file, ideally in the root of your application repo. This file tells Elastic Beanstalk how to pull and run the Docker image from ECR and it will look like:

    {
      "AWSEBDockerrunVersion": "1",
      "Image": {
        "Name": "aws_account_id.dkr.ecr.region.amazonaws.com/repository_name:tag",
        "Update": "true"
      },
      "Ports": [
        {
          "ContainerPort": "80"
        }
      ]
    }
    

    Replace aws_account_id, region, repository_name, and tag with your actual AWS account ID, region, ECR repository name, and image tag.

    Then to actually deploy your docker container using the Elastic Beanstalk CLI:

    • First, ensure you have the EB CLI installed.
    • Navigate to the directory containing your Dockerrun.aws.json.
    • Initialize your Elastic Beanstalk application using eb init and follow the prompts to set up your application.
    • Create an environment and deploy your application using eb create and eb deploy.

    To deploy it using the AWS Management Console:

    • Zip the Dockerrun.aws.json file on your machine.
    • Go to the Elastic Beanstalk console and create a new application.
    • When prompted to upload code, choose "Upload your code" and upload the zipped Dockerrun.aws.json file.

    To learn more about using Docker with Elastic Beanstalk, you can refer to the official AWS documentation: Using Docker with AWS Elastic Beanstalk