Getting Started with Amazon Elastic Container Service

   Go back to the Task List

  « 3: Create Cloud9 Environment    5: Launch ECS Cluster »

4: Create Docker Image

You create a Docker image and then upload to the AWS ECR repository. You will create Docker image with a sample python file app.py which deploys a Flask application.

  1. In AWS Cloud9 IDE, create a file app.py and copy paste the following code. The code is running a simple Flask application. The 0.0.0.0 is a wildcard IP address that will match any possible incoming port on the host machine. The application port is changed to 80 from the default 5000.

    Cloud9

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return "Welcome to Dummy Dojo Web Page"
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0", port=80)
    

    `

  2. In AWS Cloud9 IDE, create another file with the name Dockerfile and copy paste the following script. The script is using Python:3 as the base image from the DockerHub. Copies app.py file to the root. Installs Flask python package and finally runs app.py file.

    Cloud9

    FROM python:3
    
    ADD app.py /
    
    RUN pip install Flask
    
    ENTRYPOINT python app.py
    

    `

  3. The files are ready. You will use the push commands from the AWS ECR repository to create and upload the Docker image. Run the following command in the console to authenticate to the ECR registry. Replace with the code of the region you are using for the workshop. Replace with the account number of the AWS account you are using.

        aws ecr get-login-password --region <Region-Code> | docker login --username AWS --password-stdin <Account-Number>.dkr.ecr.<Region-Code>.amazonaws.com
    

    `

    Cloud9

  4. Next you run the following command in the console to create the Docker image. There is a dot in the end of the command, copy the complete command.

     docker build -t dojorepo .
    

    ` Cloud9

  5. Next you run the following command to tag the image so you can push the image to the repository. Replace with the code of the region you are using for the workshop. Replace with the account number of the AWS account you are using.

     docker tag dojorepo:latest <Account-Number>.dkr.ecr.<Region-Code>.amazonaws.com/dojorepo:latest
    

    `

  6. Finally, run the following command to push the image to the AWS ECR repository. Replace with the code of the region you are using for the workshop. Replace with the account number of the AWS account you are using.

     docker push <Account-Number>.dkr.ecr.<Region-Code>.amazonaws.com/dojorepo:latest
    

    ` Cloud9

  7. The Docker image has been pushed to the AWS ECR repository. You can verify it by opening the dojorepo repository in the AWS ECS console. Make note of the Image URI as you need it later when configuring container in Amazon ECS.

    Cloud9

  8. The image is ready. Let’s start configuration of the ECS cluster and launch application based on the image.