Docker Deployment

Developing your Docker Image Locally

  1. Every time you make changes to any part of your Docker files (e.g., Dockerfile, app.py, requirements.txt), you need to build a new Docker image. Run the following command. Note that my-local-docker-image is the image name in the example but you should provide a more meaningful name.

    docker build ./ -t my-local-docker-image
Hint

Try running the command docker images to see the images you have in your local environment.

  1. To run your newly built Docker container, run the command:

    docker run my-local-docker-image
  2. If you need to interact with your container directly, run a command like:

    ```{bash}
    docker run -it --entrypoint=/bin/bash my-local-docker-image
    ```
  3. Once you are satisfied with local testing, then you may want to push that image to an external repository like ECR.

Posting Docker Image to ECR

ECR stands for Elastic Container Registry.

  1. Run the command to make a new repo in the elastic container registry to store your new containers. The repository name, in the example my-docker-repo, needs to specified in a few places.
aws ecr create-repository --repository-name my-docker-repo
  1. Run the commands to grab info on your AWS account and region.

    aws_region=$(aws configure get region)
    aws_account_id=$(aws sts get-caller-identity --query 'Account' --output text)
  2. Run the following command to configure your authentication to talk to the ECR service. Note how we use BASH variable with the $ so that you don’t have to manually enter your region or account id.

    aws ecr get-login-password \
    --region $aws_region \
    | docker login \
    --username AWS \
    --password-stdin $aws_account_id.dkr.ecr.$aws_region.amazonaws.com
  3. Tag the image in the ECR registry by running the command. The final my-docker-repo is referring to the new repository you just built a few commands ago. The my-local-docker-image is the tag in your local Docker environment for the image you want to push.

    docker tag my-local-docker-image $aws_account_id.dkr.ecr.$aws_region.amazonaws.com/my-docker-repo
  4. Push the image to docker by running the command

    docker push $aws_account_id.dkr.ecr.$aws_region.amazonaws.com/my-docker-repo

Read more about pushing a Docker image to ECR here.