Skip to content

Instantly share code, notes, and snippets.

@jcyuyi
Last active March 13, 2019 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcyuyi/7d36d16003d95692bcfbdc068c351aab to your computer and use it in GitHub Desktop.
Save jcyuyi/7d36d16003d95692bcfbdc068c351aab to your computer and use it in GitHub Desktop.
SpringBoot Dockerfile using docker-entrypoint
#!/bin/sh
set -e
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts # FIX ECS hosts issue
exec "$@"
FROM openjdk:8-jdk-alpine
ENV JAVA_OPTS=""
VOLUME /tmp
ARG DEPENDENCY=api/build/dependency
COPY docker-entrypoint.sh /usr/local/bin/
COPY ${DEPENDENCY}/BOOT-INF/lib /api/lib
COPY ${DEPENDENCY}/META-INF /api/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /api
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["sh", "-c", "java $JAVA_OPTS -cp api:/api/lib/* xxx.xxx.xxx.App"]
AWSTemplateFormatVersion: "2010-09-09"
Description: 'SpringBoot + Fargate service & task template'
Parameters:
stack:
Description: 'Name of an active CloudFormation stack that contains the core resources'
Type: String
ecsStack:
Description: 'Name of an active CloudFormation stack that contains the ecs resources'
Type: String
taskDesiredCount:
Description: 'The number of simultaneous tasks'
Type: Number
executionRoleArn:
Description: 'The task execution role to run task'
Type: String
taskRoleArn:
Description: 'The task role to call AWS API'
Type: String
containerName:
Description: 'The name of the container to use with the load balancer'
Type: String
containerPort:
Description: 'The port number on the container to direct load balancer traffic to'
Type: Number
image:
Description: 'The image of the container'
Type: String
cpu:
Description: 'The cpu of the container'
Type: String
family:
Description: 'The family of the container'
Type: String
memory:
Description: 'The memory of the container'
Type: String
springProfilesActive:
Description: 'SPRING_PROFILES_ACTIVE environment'
Type: String
logGroup:
Description: 'AWS log group'
Type: String
logRegion:
Description: 'AWS log region'
Type: String
ddApiKey:
Description: 'DataDog API KEY'
Type: String
Resources:
taskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ExecutionRoleArn: !Ref executionRoleArn
Cpu: !Ref cpu
Family: !Ref family
Memory: !Ref memory
TaskRoleArn: !Ref taskRoleArn
NetworkMode: 'awsvpc'
RequiresCompatibilities:
- 'FARGATE'
ContainerDefinitions:
- Name: !Ref containerName
Image: !Ref image
PortMappings:
- ContainerPort: !Ref containerPort
HostPort: !Ref containerPort
Environment:
- Name: 'SPRING_PROFILES_ACTIVE'
Value: !Ref springProfilesActive
- Name: 'JAVA_OPTS'
Value: '-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=2003 -Dcom.sun.management.jmxremote.rmi.port=2003'
Essential: 'true'
DockerLabels:
com.datadoghq.ad.check_names: '["jmx"]'
com.datadoghq.ad.instances: '[{"jmx_url":"service:jmx:rmi:///jndi/rmi://localhost:2003/jmxrmi"}]'
com.datadoghq.ad.init_configs: '[{}]'
LogConfiguration:
LogDriver: 'awslogs'
Options:
awslogs-group: !Ref logGroup
awslogs-region: !Ref logRegion
awslogs-stream-prefix: 'ecs'
- Name: 'datadog-agent'
Image: 'datadog/agent:latest-jmx'
Environment:
- Name: 'DD_API_KEY'
Value: !Ref ddApiKey
- Name: 'ECS_FARGATE'
Value: 'true'
- Name: 'SD_JMX_ENABLE'
Value: 'true'
Essential: 'true'
ecsService:
Type: AWS::ECS::Service
Properties:
Cluster:
Fn::ImportValue:
!Sub "${ecsStack}-ecsCluster"
DesiredCount: !Ref taskDesiredCount
HealthCheckGracePeriodSeconds: 180
LaunchType: 'FARGATE'
LoadBalancers:
- ContainerName: !Ref containerName
ContainerPort: !Ref containerPort
TargetGroupArn:
Fn::ImportValue:
!Sub "${ecsStack}-tgECSArn"
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: 'DISABLED'
SecurityGroups:
- Fn::ImportValue:
!Sub "${stack}-securityGroupECSService"
Subnets:
- Fn::ImportValue:
!Sub "${stack}-privateSubnet1"
- Fn::ImportValue:
!Sub "${stack}-privateSubnet2"
ServiceName: !Sub "${stack}-ecsService"
TaskDefinition: !Ref taskDefinition
Outputs:
taskDefinition:
Description: 'Task Definition'
Value: !Ref taskDefinition
ecsService:
Description: 'ECS Service'
Value: !Ref ecsService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment