If you have not created the container repository as part Lab II, complete the Create container repository section of Lab II before proceeding.
In this section, you will create a Docker container for the application and a buildspec file in the CodeCommit repository created in the previous section
A buildspec is a collection of build commands and related settings in YAML format. This file is used by AWS CodeBuild to automatically create an updated version of the container upon code changes. The buildspec file informs CodeBuild of all the actions that should be taken during a build run for your application. In the next section, you will dive deeper on what is CodeBuild and how to set it up as part of a pipeline.
pwd # should be MyDemoRepo
cat > Dockerfile << EOF
FROM public.ecr.aws/amazonlinux/amazonlinux:latest
ADD script.py /
CMD python /script.py
EOF
cat > buildspec.yml << EOF
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- \$(aws ecr get-login --region \$AWS_REGION --no-include-email)
- IMAGE_TAG=\$(echo \$CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-8)
- echo IMAGE TAG \$IMAGE_TAG
build:
commands:
- echo Build started at \$(date)
- echo Building the Docker image...
- docker build -t \$REPOSITORY_URI:latest .
- docker tag \$REPOSITORY_URI:latest \$REPOSITORY_URI:\$IMAGE_TAG
post_build:
commands:
- echo Build completed at $(date)
- echo Pushing the Docker images...
- docker push \$REPOSITORY_URI:latest
- docker push \$REPOSITORY_URI:\$IMAGE_TAG
EOF
cat > script.py << EOF
# Hello World Python Script
print('Hello World!')
EOF
git add Dockerfile buildspec.yml script.py
git commit -m "Created Dockerfile and buildspec file"
git push origin main
main
:aws codecommit update-default-branch --repository-name MyDemoRepo --default-branch-name main --region $AWS_REGION