-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-task.sh
106 lines (95 loc) · 3.02 KB
/
docker-task.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Helper scripts for working with Docker image and container.
# Author: Dylan Funk
# Email: [email protected]
# Variables
export IMAGE_NAME="dylan_funk_personal_backend"
CONTAINER_NAME="personal-site-flask"
AWS_REGION="us-east-2"
AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID # "01234567890" a 12 digit number, from top right of console.
REPOSITORY_PATH="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com" #TODO: set this to the path of your ECS repository
FULLY_QUALIFIED_IMAGE_NAME="$REPOSITORY_PATH/$IMAGE_NAME"
HOST_PORT=9999
CONTAINER_PORT=8080
# Get version from package.json file so can tag the built image with version number.
# If you don't have node installed, you can just hardcode the version number here.
IMAGE_VERSION=3
# Builds the Docker image and tags it with latest version number.
buildImage () {
echo Building Image Version: $IMAGE_VERSION ...
cd backend
docker build -t $IMAGE_NAME:latest -t $IMAGE_NAME:$IMAGE_VERSION -f Dockerfile . \
&& echo Build complete.
}
# Pushes the latest version of the image both with the `latest` and specific version tags
pushImage () {
docker tag "$IMAGE_NAME:latest" "$FULLY_QUALIFIED_IMAGE_NAME:latest"
docker tag "$IMAGE_NAME:$IMAGE_VERSION" "$FULLY_QUALIFIED_IMAGE_NAME:$IMAGE_VERSION"
aws ecr get-login-password --region "$AWS_REGION" | \
docker login \
--username AWS \
--password-stdin "$REPOSITORY_PATH"
docker push "$FULLY_QUALIFIED_IMAGE_NAME:latest"
docker push "$FULLY_QUALIFIED_IMAGE_NAME:$IMAGE_VERSION"
}
createRepo () {
aws ecr create-repository --repository-name $IMAGE_NAME
echo Created ECR repository: $IMAGE_NAME.
}
runContainer () {
docker-compose up
}
showImage () {
echo "Your ECR fully qualified image name is:"
echo "$FULLY_QUALIFIED_IMAGE_NAME"
}
# Shows the usage for the script.
showUsage () {
echo "Description:"
echo " Builds, runs and pushes Docker image '$IMAGE_NAME'."
echo ""
echo "Options:"
echo " build: Builds a Docker image ('$IMAGE_NAME')."
echo " run: Runs a container based on an existing Docker image ('$IMAGE_NAME')."
echo " buildrun: Builds a Docker image and runs the container."
echo " createrepo: Creates new ECR repo called '$IMAGE_NAME'"
echo " push: Pushs the image '$IMAGE_NAME' to an image repository"
echo ""
echo "Example:"
echo " ./docker-task.sh build"
echo ""
echo " This will:"
echo " Build a Docker image named $IMAGE_NAME."
}
if [ $# -eq 0 ]; then
showUsage
else
case "$1" in
"build")
buildImage
;;
"run")
runContainer
;;
"buildpush")
buildImage
pushImage
;;
"push")
pushImage
;;
"createrepo")
createRepo
;;
"buildrun")
buildImage
runContainer
;;
"showimage")
showImage
;;
*)
showUsage
;;
esac
fi