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.
-
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.
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)
`
-
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.
FROM python:3 ADD app.py / RUN pip install Flask ENTRYPOINT python app.py
`
-
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
`
-
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 .
`
-
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
`
-
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
`
-
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.
-
The image is ready. Let’s start configuration of the Fargate ECS cluster and launch application based on the image.