docker build not taking the updated dockerfile - spring-boot

I am trying to build an image from a dockerfile that I have. However, I dont know why the image does not take the updated changes from the dockerfile, although I have tried using the --no-cache option.
the dockerfile looks as under for me
FROM openjdk:17-jdk-slim-buster
WORKDIR /app
COPY target/vulnerabilitymanager-1.0.jar /app
WORKDIR /app
ENV VERSION=1.0
CMD ["java", "-Dserver.servlet.context-path=/vulnerabilitymanager", "-jar", "/app/vulnerabilitymanager1-${VERSION}.jar"]
When I build the DockerFile using the following command
docker build --no-cache -t vuln:1.0 .
I still see the start command as follows when I inspect the container
[
{
"Id": "3138d0696c5e5b3eb41f6d166b69b4a24ba3548189cf00e4092cd37862f80488",
"Created": "2022-04-22T07:34:22.930767Z",
"Path": "/bin/sh",
"Args": [
"-c",
"java -jar /app/vulnerabilitymanager-${VERSION}.jar"
],
As you can see, I cannot see the command line arguments that I have passed in CMD in the Dockerfile.

Related

Docker: Change port number during runtime

FROM openjdk:8-jre-alpine
COPY --from=builder tmp/target/Application*.jar app.jar
RUN mkdir -p /app
ARG specified_port=8090
ENV EXPOSED_PORT=$specified_port
EXPOSE $EXPOSED_PORT
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "app.jar"]
And during the runtime how can i change the ports during docker run
server.port to 8092
sample docker run
docker run --net="host" -p 8092:8092 -p 9992:9992 4794973497437
Using Environment Variables
Add this in dockerfile
ENV PORT 8090
then run
docker run -d image_name -e "PORT=8092"
or Another way is
ARG PORT
and use it like
CMD [ $PORT]
and then run your docker container like this
export PORT=8092; docker run -d image_name"

Spring boot cant find application-$profile.properties file: Docker file

Docker file: Spring unable to find profile.properties file
FROM java:8u111-jre
ARG VERSION
ARG PROFILE
RUN mkdir /app
WORKDIR /app
RUN chown 1001:0 -R /app
RUN chmod 777 /app
COPY target/jira2bd-$VERSION.jar app.jar
USER 1001
CMD java $JAVA_OPTS -jar /app.jar --spring.profiles.active=$PROFILE
EXPOSE 8080
How I know Docker ARGs are not allowed in CMD instruction.
The solution is to convert them into the ENV variables. Try something like that
ARG PROFILE
ENV PROFILE ${PROFILE}
CMD java ${JAVA_OPTS} -jar /app.jar --spring.profiles.active=${PROFILE}
Also take a look here Is Docker ARG allowed within CMD instruction

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.

docker image syntax error

When I run docker command on my image, it errors out. Any ideas on what should I include in my Dockerfile for CMD? Currently, I have java -jar xxxx.jar in it.
here is my Dockerfile:
FROM openjdk:8-jre-alpine
COPY ./target/hola-docker-1.0.0-SNAPSHOT.jar D:/docker-built-test
WORKDIR -p D:/docker-built-test
EXPOSE 8080
CMD ["java", "-jar", "hola-docker-1.0.0-SNAPSHOT.jar"]
My command docker build -t hello-manual-build1 .
click here Created Docker Image
When am trying to create a container using the new image then facing an error don't know how to resolve please help me to move forward
Thanks

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