spring boot container not running because of dependancy on postgres container - java

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.

Related

How can I access the volume I created in docker compose via dockerfile?

I have created a volume in docker compose and want to copy files into it. I need docker file for this. However, I don't know how to access the volume I created in docker compose via dockerfile. How can I do it?
docker-compose.yml
version: '3'
services:
postgres:
image: postgres
hostname: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
pgadmin:
image: dpage/pgadmin4
restart: always
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: test#gmail.com
PGADMIN_DEFAULT_PASSWORD: test
depends_on:
- postgres
web:
build:
context: ../
dockerfile: spring/Dockerfile
ports:
- "8080:8080"
depends_on:
- postgres
volumes:
- ./test-volume:/opt/_LIB/
Dockerfile
FROM ...
RUN rm -rf /var/lib/jetty/webapps/*
COPY spring/target/*.war /var/lib/jetty/webapps/
EXPOSE 8080
#CMD ["java", "-jar", "$JETTY_HOME/start.jar"]
[How can I] access the volume I created in docker compose via dockerfile?
You can't. It doesn't exist yet. Everything in the Compose build: block happens before any other option in the Compose file is considered; you don't get to see volumes:, or environment: variables, or access other containers on networks: (even the automatically-provided default network).
The way the postgres image handles this is through an entrypoint wrapper script that creates the database if it doesn't exist. You can write a similar script:
#!/bin/sh
# entrypoint.sh
# If the data files don't exist yet, copy them in
if [ ! -f /opt/_LIB/data.txt ]; then
java -cp "$JETTY_HOME/start.jar" com.example.tools.SeedData /opt/_LIB
fi
# Run the main container command
exec "$#"
Replace the java command with whatever invocation would copy the data files from somewhere else in the image into the volume; even just a cp -r is fine, so long as the source data is somewhere other than the data directory. You can similarly change the logic to overwrite the old content if it exists, or if you have some way to merge the old and new files, you can do that too.
In your Dockerfile you can then leave your CMD as-is, but make sure to COPY this script into your image and make it the ENTRYPOINT.
FROM ...
RUN rm -rf /var/lib/jetty/webapps/*
COPY spring/target/*.war /var/lib/jetty/webapps/
COPY entrypoint.sh /usr/local/bin # <-- ADD
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] # <-- ADD
CMD ["java", "-jar", "/var/lib/jetty/webapps/start.jar"] # unmodified
The ENTRYPOINT line must use JSON-array syntax (which in turn means it can't include environment-variable references; I've corrected this in the CMD). The script needs to be executable (run chmod +x entrypoint.sh on the host system if needed).
(There is one corner case around Docker named volumes that some questions will try to take advantage of, but I don't generally recommend it. In your example the /opt/_LIB container directory uses a bind-mounted host directory so this trick won't work; it also doesn't work if you ever want to change the content in the image, or if you're deploying to an environment that's not native Docker.)

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.

Can't connect Java and MySQL on docker

When I run the docker compose, it returns the error
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
I don't have much experience with docker.. what am i doing wrong?
My docker compose:
version: '3.5'
services:
mysql-service:
image: mysql:5.7
networks:
- phonebook-network
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=PhoneBook
restart: on-failure
phonebook-service:
build:
context: ./
args:
JAR_FILE: ./Phonebook-0.0.1-SNAPSHOT.jar
ports:
- "8080:8080"
environment:
- DB_HOST=jdbc:mysql://mysql-service:3306/PhoneBook?useTimezone=true&serverTimezone=UTC&useSSL=false
- DB_USERNAME:admin
- DB_PASSWORD:admin
networks:
- phonebook-network
depends_on:
- mysql-service
restart: on-failure
networks:
phonebook-network:
driver: bridge
My dockerfile:
FROM openjdk:11-jdk-slim
VOLUME /phoneBook
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
First, ensure the ports are exposed in MySQL as well:
services:
mysql-service:
image: mysql:5.7
networks:
- phonebook-network
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=PhoneBook
restart: on-failure
the "depends_on" config dont resolve the problem about the order that the containers start?
Yes, the depends_on ensures that the MySQL container will start before the Java one, but it might happen that MySQL itself takes a long time to bootstrap, so the container is ready but the process inside is not.
If the problem still persists, this can be a possible solution: create a docker_entrypoint.sh file at the root of your project. This also assumes the container has netcat installed.
#!/usr/bin/env bash
echo "Waiting for MySQL..."
# I'm assuming you're running it in a default port (3306)
until nc -vz "mysql-service" 3306; do
>&2 echo "Waiting for MySQL to be available..."
sleep 0.1
done
echo "MySQL started"
java -jar /app.jar
then modify your Dockerfile:
FROM openjdk:11-jdk-slim
VOLUME /phoneBook
ARG JAR_FILE
RUN apt-get update --fix-missing \
&& apt-get install -y --no-install-recommends netcat \
&& rm -rf /var/lib/apt/lists/*
COPY ${JAR_FILE} app.jar
COPY ./docker_entrypoint.sh docker_entrypoint.sh
RUN chmod 100 docker_entrypoint.sh
ENTRYPOINT [ "./docker_entrypoint.sh" ]
I don't have a way to test this right now, but let me know if this works.

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

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

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"]

Categories

Resources