How to measure Docker startup time - spring

I'm buiding a docker image from a jar file ( Spring Boot application), but I could not use the command time docker run my-image to check the startup time of the container. Because of the output from Spring Boot, the docker run command will not stop.
The Dockerfile:
FROM openjdk:8-jre-alpine
VOLUME /tmp
ADD target/myApp-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar" ]
Is there other way to measure the startup time of the container?
Thanks in advance!

Related

How to dockerize Spring Boot Application?

I want to dockerize my Spring Boot Project.
In eclipse, I have already created a Dockerfile (after creating the application jar file) like this:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/device-fixer-1.2.1.jar.jar
COPY ${JAR_FILE} device-fixer-1.2.1.jar.jar
ENTRYPOINT ["java","-jar","/device-fixer-1.2.1.jar.jar"]
Now what should I do to be able to dockerize?
Assuming your app listens on port 80
docker build -t <your-app-name> .
docker run -d -p 80:80 <your-app-name>
Create files that are named as Dockerfile and docker-compose.yml under project root directory.
Dockerfile
FROM openjdk:11-slim
COPY /target/app.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
version: '1'
services:
app:
build:
context: "./"
dockerfile: Dockerfile
container_name: 'app-name'
hostname: app-name
ports:
- '8080:8080'
Run docker-compose up -d command under root directory to create an image and run your application on localhost:8080

unable to run docker container as getting this error:Unable to access jarfile app.jar

I have created the image using below Docker file.Its working fine in local when I run it in the container using this image[docker run -p 80:8080 username/spring-boot-docker-aws-demo:tag
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
I have pushed this image to Docker hub to run it in EC2 instance.when I run it its saying "Error: Unable to access jarfile app.jar"
https://hub.docker.com/layers/chaituu/spring-boot-docker-aws-demo/docker-aws-demo-tag/images/sha256-110363a016eb2250264d96c8890dede518d8e519fdc9a27174334fe1096a540e?context=repo
what could be the issue?
I had a similar issue today (although in my case it wouldn't even run locally). Here's what your Dockerfile would look like with my fix:
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["sh"]
CMD ["-c", "java -jar /app/app.jar"]

How to add storage using Volume in a Docker container

I have a spring boot application and I want to start it on a docker container. However, I want to persist data, so I do not have to rebuild image to see the changes I made to RestAPI.
My docker file
FROM maven:3.6.1-jdk-8-alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY . /usr/src/
RUN mvn clean install
WORKDIR /usr/src/target
ENTRYPOINT ["java", "-jar", "SecurityAndDocker-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080:8080
Image build is ok
Running docker run -p 8080:8080 image-name works perfectly fine
How would I attach a volume to this application?
Thank you!
you can mount the volume, while running the container.
docker run -d -p 8080:8080 --name <container-name> --mount source=myvol2,target=/app <image-name>
you can also read the documentation: https://docs.docker.com/storage/volumes/
Feel free to ask in case you found any issue.
If I understand you want the target directory to be extracted out in a volume from your custom image's container.
use VOLUME keyword to specify the directory.
Your dockerfile should go like this.
FROM maven:3.6.1-jdk-8-alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY . /usr/src/
RUN mvn clean install
VOLUME /usr/src/target
WORKDIR/usr/src/target
ENTRYPOINT ["java", "-jar", "SecurityAndDocker-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080:8080
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If you want to save the working directory just add VOLUME in your dockerfile.

Why does my Spring Boot App hang when started in a Docker Swarm

I created a docker image of a Spring Boot app and when I run it using
docker run -d -p 4000:8080 stanlick/stats:v1
It works great! However, when I try to run it as a swarm (even with replicas:1) it hangs on startup.
Running
docker service logs play-ball_web
reveals:
Dockerfile:
FROM openjdk:12-jdk-alpine
VOLUME /tmp ARG JAR_FILE
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Passing env variables to DOCKER Spring Boot

I have a SpringBoot application and its Dockerfile is as follows. I have application.properties for different environments like local/dev/qa/prod. When I run the application locally in IDE, I pass -Dspring.profiles.active=local in VM options so that it loads the application-local.properties. For running as docker containers, I build an image which comprises of all the application.properties. i.e. it's only SAME docker image for all the environments.
When I run the image in an environment, I want to somehow make the SpringBoot to understand that its dev env, so it has to load application-dev.properties. I am using AWS ECS for managing the containers.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/sample-test-sb-sample-app-1.0-exec.jar app.jar
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
The easiest (and probably the best way) to do it via environment variable in a docker container:
SPRING_PROFILES_ACTIVE=dev,swagger
UPDATE:
In order to set environment variables to docker, you do not need to modify Dockerfile. Just build your docker image and then run it with the env variables set:
docker run your-docker-container -e SPRING_PROFILES_ACTIVE='dev,swagger' -p 8080:8080
In the .Dockerfile file:
ENTRYPOINT [ "sh", "-c", "java -Dspring.profiles.active=**${ENV}** -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
And while running the docker:
docker run --env ENV=*local* -d -p 8080:8080 <*image id*>
This way, the environment variable gets local as value and passes to Dockerfile when we bring up a container.
Update
You can also do like
ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=${ENV} -Djava.security.egd=file:/dev/./urandom","app.jar"]
and while docker image
docker run --env ENV=local -d -p 8080:8080 <*image id*>

Resources