Skip to content

Commit 861fd19

Browse files
committed
Deployed to ECS successfully
1 parent af8ff7c commit 861fd19

File tree

6 files changed

+93
-58
lines changed

6 files changed

+93
-58
lines changed

grafana/non-prod/docker/Dockerfile

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,27 @@ FROM grafana/grafana:latest
33

44
# Install Python
55
USER root
6-
RUN apk add --no-cache python3 py3-pip
6+
RUN echo "Installing Python..." \
7+
&& apk add --no-cache python3 py3-pip \
8+
&& echo "Python installed successfully."
79

810
# Copy provisioning and dashboards
11+
RUN echo "Copying provisioning files..."
912
COPY ./provisioning /etc/grafana/provisioning
10-
COPY ./dashboards /var/lib/grafana/dashboards
13+
RUN echo "Provisioning files copied."
1114

12-
# Copy the entrypoint script
13-
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
15+
RUN echo "Copying dashboard files..."
16+
COPY ./dashboards /var/lib/grafana/dashboards
17+
RUN echo "Dashboard files copied."
1418

15-
# Ensure the entrypoint script is executable
16-
RUN chmod +x /usr/local/bin/entrypoint.sh
19+
COPY run.sh /run.sh
20+
RUN chmod +x /run.sh
1721

1822
# Expose Grafana port
1923
EXPOSE 3000
2024

2125
# Switch to the non-root user provided by the base image
2226
USER grafana
2327

24-
# Uncomment and set environment variables for configuration if needed
25-
# ENV GF_AUTH_ANONYMOUS_ENABLED=true
26-
# ENV GF_AUTH_ANONYMOUS_ORG_ROLE=Viewer
27-
# ENV GF_AUTH_BASIC_ENABLED=false
28-
# ENV GF_AUTH_DISABLE_LOGIN_FORM=true
29-
# ENV GF_AUTH_DISABLE_SIGNOUT_MENU=true
30-
# ENV GF_SECURITY_ALLOW_EMBEDDING=true
31-
# ENV GF_SERVER_SERVE_FROM_SUB_PATH=true
32-
# ENV GF_SERVE_FROM_SUB_PATH=true
33-
34-
# Set the entrypoint script
35-
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
36-
37-
# Set the default command
38-
CMD ["grafana-server", "--homepath=/usr/share/grafana"]
28+
ENTRYPOINT ["/run.sh"]
29+
# CMD ["grafana-server", "--homepath=/usr/share/grafana"]
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
# docker/build_push_to_ecr.sh
2-
31
#!/bin/bash
42

53
# Set variables
4+
5+
dirname=$(dirname "$0")
6+
DOCKERFILE_DIR=$(realpath "$dirname")
7+
echo "DOCKERFILE_DIR: $DOCKERFILE_DIR"
8+
69
AWS_REGION="eu-west-2"
710
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
811
REPOSITORY_NAME="imms-fhir-api-grafana"
912
IMAGE_TAG="11.0.0-22.04_stable"
10-
DOCKERFILE_DIR="/Users/watess01/Documents/NHS/code/immunisation-fhir-api/grafana/non-prod/docker"
13+
LOCAL_IMAGE_NAME="$REPOSITORY_NAME:$IMAGE_TAG"
14+
IMAGE_NAME="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$LOCAL_IMAGE_NAME"
1115
TAGS="Key=Environment,Value=non-prod Key=Project,Value=immunisation-fhir-api-grafana"
16+
LIFECYCLE_POLICY_FILE="lifecycle-policy.json"
1217

1318
# Change to the directory containing the Dockerfile
1419
cd $DOCKERFILE_DIR
@@ -29,42 +34,31 @@ if [ $? -ne 0 ]; then
2934
aws ecr tag-resource --resource-arn arn:aws:ecr:$AWS_REGION:$ACCOUNT_ID:repository/$REPOSITORY_NAME --tags $TAGS
3035
fi
3136

37+
# Apply lifecycle policy to the ECR repository
38+
aws ecr put-lifecycle-policy --repository-name $REPOSITORY_NAME --lifecycle-policy-text file://$LIFECYCLE_POLICY_FILE --region $AWS_REGION
39+
3240
printf "Building and pushing Docker image to ECR...\n"
3341
# Authenticate Docker to ECR
3442
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
3543

3644
printf "Building Docker image...\n"
37-
# Build Docker image
38-
# docker build -t $REPOSITORY_NAME:$IMAGE_TAG .
39-
# Build Docker image for linux/amd64 architecture
40-
docker buildx create --use
41-
docker buildx build --platform linux/amd64 -t $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REPOSITORY_NAME:$IMAGE_TAG --push .
45+
# Remove existing Docker image if it exists
46+
docker rmi $IMAGE_NAME --force
47+
48+
# Pull the base image for linux/amd64 architecture
49+
docker pull --platform linux/amd64 grafana/grafana:latest
4250

51+
# Build Docker image for linux/amd64 architecture and push to ECR
52+
docker buildx create --use
53+
docker buildx build --platform linux/amd64 -t $IMAGE_NAME --push .
4354

4455
# Check if the build was successful
4556
if [ $? -ne 0 ]; then
4657
echo "Docker build failed."
4758
exit 1
4859
fi
4960

50-
printf "Tagging Docker image...\n"
51-
# Tag Docker image
52-
docker tag $REPOSITORY_NAME:$IMAGE_TAG $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REPOSITORY_NAME:$IMAGE_TAG
53-
54-
# Check if the tag was successful
55-
if [ $? -ne 0 ]; then
56-
echo "Docker tag failed."
57-
exit 1
58-
fi
59-
60-
printf "Pushing Docker image to ECR...\n"
61-
# Push Docker image to ECR
62-
docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REPOSITORY_NAME:$IMAGE_TAG
63-
64-
# Check if the push was successful
65-
if [ $? -ne 0 ]; then
66-
echo "Docker push failed."
67-
exit 1
68-
fi
61+
# Inspect the built image
62+
echo "Image: $LOCAL_IMAGE_NAME"
6963

7064
echo "Docker image built and pushed to ECR successfully."

grafana/non-prod/docker/entrypoint.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"rules": [
3+
{
4+
"rulePriority": 1,
5+
"description": "Retain only the latest image",
6+
"selection": {
7+
"tagStatus": "any",
8+
"countType": "imageCountMoreThan",
9+
"countNumber": 1,
10+
"tagPrefixList": [""]
11+
},
12+
"action": {
13+
"type": "expire"
14+
}
15+
}
16+
]
17+
}

grafana/non-prod/docker/readme.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
# To Build and deploy Grafana Docker image to AWS ECR
1+
# Docker
2+
3+
## Introduction
4+
This docker folder is used to deploy a grafana docker image to AWS ECR for use by ECS
5+
6+
## architecture
7+
1. Dockerfile uses grafana/grafana:latest and is built for linux/amd64 for deploy to ECS
8+
2. Entrypoint script `run.sh` starts grafana in a controlled manner and permits debug on startup
9+
10+
## To Build and deploy Grafana Docker image to AWS ECR
211

312
1. Start docker
413
2. execute `build_push_to_ecr.sh`
5-
3. Success message "Docker image built and pushed to ECR successfully."
14+
3. Success message "Docker image built and pushed to ECR successfully."

grafana/non-prod/docker/run.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
echo "Grafana Docker container startup..."
3+
4+
GRAFANA_SERVER_PATH=$(which grafana-server)
5+
if [ -z "$GRAFANA_SERVER_PATH" ]; then
6+
echo "grafana-server not found. Exiting."
7+
exit 1
8+
else
9+
echo "grafana-server found at $GRAFANA_SERVER_PATH"
10+
fi
11+
12+
echo "Starting Grafana with environment variables:"
13+
env | grep GF_
14+
15+
# Check if Grafana configuration exists
16+
if [ ! -f /etc/grafana/grafana.ini ]; then
17+
echo "Grafana configuration not found. Exiting."
18+
exit 1
19+
fi
20+
21+
which grafana-server
22+
23+
# Start Grafana in the foreground
24+
echo "Starting Grafana server..."
25+
exec $GF_PATHS_HOME/bin/grafana-server \
26+
--homepath=$GF_PATHS_HOME \
27+
--config=/etc/grafana/grafana.ini \
28+
--packaging=docker \
29+
cfg:default.paths.data=/var/lib/grafana \
30+
cfg:default.paths.logs=/var/log/grafana \
31+
cfg:default.paths.plugins=/var/lib/grafana/plugins \
32+
cfg:default.paths.provisioning=/etc/grafana/provisioning

0 commit comments

Comments
 (0)