How can I start spring boot application in docker with profile? - java

I have a simple spring-boot project:
-resources
-application.yaml
-application-test.yaml
And I have this Dockerfile:
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ADD micro-boot.jar micro-boot.jar
ENTRYPOINT ["java","-Dspring.profiles.active=test" "-jar","/micro-boot.jar"]
1) I build image - C:\micro-boot>docker build -f Dockerfile -t micro-boot .
2) show all images - C:\micro-boot>docker image ls -a
micro-boot latest ccc9a75ebc24 4 seconds ago 112MB
3) try to start C:\micro-boot>docker image ls -a
And I get an error:
/bin/sh: [java,-Dspring.profiles.active=test: not found

We have 3 ways:
1. Passing Spring Profile in a Dockerfile
FROM openjdk:8-jre-alpine
...
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=test","-jar","app.jar"]
2. Passing Spring Profile in Docker run
docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=test" --name my-app:latest
3. Passing Spring Profile in DockerCompose
version: "3.5"
services:
my-app:
image: my-app:latest
ports:
- "8080:8080"
environment:
- "SPRING_PROFILES_ACTIVE=test"

There's a typo here
ENTRYPOINT ["java","-Dspring.profiles.active=test" comma missing here "-jar","/micro-boot.jar"]

Related

Intellij idea use docker-compose can't mappeed native file to the remote docker

I just want to deploy my springboot applicaiton on the remote centos server through the intellij idea.
First I tried the way of DockerFile to deploy the application on intellij idea and I successed.
this is my DockerFile config
FROM williamyeh/java8
# COPY or ADD to image
COPY dockerdemo-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c "touch /app.jar"
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
but when I tried the method of docker-compose.yml,I failed,I can't mapped my navtive jar to the remote system.my docker-compose.yml config is
version: "3"
services:
dockerDemo2:
image: adoptopenjdk/openjdk8:latest
container_name: dockerDemo2
privileged: true
environment:
TZ: Asia/Shanghai
LANG: es_US.UTF-8
volumes:
- ./dockerdemo-0.0.1-SNAPSHOT.jar:/docker/dockerdemo-0.0.1-SNAPSHOT.jar
command: bash -c " tail -f /dev/null "
ports:
- 8080:8080
I deployed successed,but when I get in the remote docker ,I found the /docker/dockerdemo-0.0.1-SNAPSHOT.jar is a directory, It should expected as a jar file,but it not,I don't know why that.that's really confuesed me.

docker-compose failed to build on windows 10

I'm trying to deploy my spring boot application with docker compose but get this error:
Step 14/15 : COPY ${JAR_FILE} manager.jar
ERROR: Service 'manager' failed to build : When using COPY with more than one source file, the destination must be a directory and end with a /
But if I do a docker build using the Dokerfile it works correctly. The question is, why fail with docker-compose up?.
C:\Push\Workspace\manager>docker build --tag "docker-manager:latest" .
[+] Building 7.3s (8/8) FINISHED
I have tried looking for examples but I am using windows 10
The Dockerfile:
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} example.jar
ENTRYPOINT ["java", "-jar", "example.jar"]
The docker-compose.yml
services:
oracle:
image: container-registry-london.oracle.com/database/enterprise:12.2.0.1
ports:
- "8080:8080"
- "1521:1521"
manager:
build: .
ports:
- "8181:8181"
depends_on:
- oracle
links:
- oracle
restart: always
I am guessing there might be mulitple jar files in your target folder.
In stead of using the *.jar , please use full name of the jar and then rebuild the dokcer compose file with --build flag.
ARG JAR_FILE=target/test-0.0.1-SNAPSHOT.jar
Then build with
docker-compose up --build

spring boot container not running because of dependancy on postgres container

I have one postgres container and one spring boot container. I am running both of them using docker-compose.
Below is the docker-compose.yml file:
version: '3'
services:
backend:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "8080:8080"
depends_on:
- postgres-db
restart: always
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres-db:5432/testDb
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
postgres-db:
working_dir: /home/models
image: postgres:11.3-alpine
restart: always
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=testDb
ports:
- "5432:5432"
volumes:
- ./pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
Here's the Dockerfile.dev
FROM maven:3-jdk-8
ENV HOME=/home/usr/app
RUN mkdir -p $HOME
WORKDIR $HOME
ADD pom.xml $HOME
RUN ["/usr/local/bin/mvn-entrypoint.sh", "mvn", "dependency:go-offline"]
ADD . $HOME
RUN ["mvn", "package"]
CMD ["java","-jar","/usr/app/testApp-1.0.0-SNAPSHOT.jar"]
and here's the application.properties file
spring.datasource.url=jdbc:postgresql://postgres-db:5432/testDb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=validate
but when I run the containers using docker-container up I am getting the error saying the connection to database failed and the container stops.
Edit:
I have added the following db-check.sh file for checking the status of db
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$#"
until PGPASSWORD="password" psql -h "$host" -U "root" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
and the new Dockerfile.dev looks like:
FROM maven:3-jdk-8
ENV HOME=/home/usr/app
RUN mkdir -p $HOME
WORKDIR $HOME
ADD db-check.sh $HOME
RUN chmod +x db-check.sh
RUN ["./db-check.sh", "postgres-db:5432", "/usr/local/bin/mvn-entrypoint.sh", "mvn", "dependency:go-offline"]
ADD . $HOME
RUN ["mvn", "package"]
CMD ["java","-jar","/usr/app/testApp-1.0.0-SNAPSHOT.jar"]
but for this script to run successfully I need psql installed in the backend container. Is there any other way of checking this because if feel that I am putting a lot of things in this single backend container for the sake of just checking the status of postgres container.
As others stated, probably the postgres-db container is not ready yet, because it has to initialzed at first run.
As a cheap workaround you can do this:
Start postgres-db container only: $ docker-compose up -d postgres-db
Wait a moment (let's say 10-20 sec)
$ docker-compose up -d --build
Hope this helps.

Docker - "image operating system "linux" cannot be used on this platform" while trying to build image

I am following this link to create docker image using spring boot project
https://dzone.com/articles/deploying-spring-boot-on-docker
Already switched to Windows Container
OS: Window 10 pro
Docker Version: 2.1.0.1
Engine: 19.03.1
While I am trying to build image i am getting this error:
A:\Projects\Docker\dockerDemo>docker build -f DockerFile -t dockerdemo .
Sending build context to Docker daemon 99.84kB
Step 1/4 : FROM java:8
8: Pulling from library/java
image operating system "linux" cannot be used on this platform
DockerFile
FROM java:8
EXPOSE 8080
ADD /target/dockerdemo.jar dockerdemo.jar
ENTRYPOINT ["java", "-jar", "dockerdemo.jar"]
Thanks in advance !!!
Instead of "java" you need "openjdk"
https://hub.docker.com/_/openjdk
This will be your Dockerfile:
FROM openjdk:[some_tag]
EXPOSE 8080
ADD /target/dockerdemo.jar dockerdemo.jar
ENTRYPOINT ["java", "-jar", "dockerdemo.jar"]
This is what i found with a java query:
https://hub.docker.com/search/?q=java&type=image

Docker Compose getting exception

I have a spring boot application which i have deployed on docker container, everything is working fine, but i want to deploy and run the application in docker container with docker compose.
This is my DockerFile
FROM java:8
VOLUME /tmp
COPY /target/order-0.0.1-SNAPSHOT.jar order.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/order.jar"]
Step 1 :- Created jar with mvn clean package
Step 2 :- docker build -t order
Step 3 :- docker run -it -d -p 8080:8080
Here everything work's fine
But if i don't execute the step 1 and want's to deploy the the application in container with docker-compose.
While trying to execute docker-compose up i am getting exception /target/order-0.0.1-SNAPSHOT.jar not found
So how to execute the mvn package command in docker-compose ?
Is their any other way's to acheive this ?
This is my docker-compose.yml
version: '3'
services:
order:
restart: always
build: ./order
working_dir: /order
volumes:
- /tmp:/logs
expose:
- "8080"
try with this one
Dockerfile
FROM maven:alpine AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
FROM openjdk:alpine
COPY --from=build /home/app/target/*.jar /usr/local/lib/demo.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/demo.jar"]
if you get error like Can't execute jar- file: “no main manifest attribute”
replace last command
ENTRYPOINT ["java","-cp","/usr/local/lib/demo.jar","com.packagename.classnamewithoutextension"]

Categories

Resources