简体   繁体   中英

Dockerizing Spring Boot App with Postgresql

I am trying to create a Docker container for a Spring Boot application which uses PostgreSQL as database. My aim is build a container which runs app as well as PostgreSQL. I created a Dockerfile as below:

FROM ubuntu:15.10
LABEL version="1"

ADD app.jar app.jar
RUN bash -c 'touch /app.jar'

#  Install Java, Postgresql
......

USER postgres
RUN /etc/init.d/postgresql start &&\
    psql --command "CREATE USER test WITH SUPERUSER PASSWORD 'test';" &&\
        createdb -O test test

USER root
RUN echo "local all all trust" >> /etc/postgresql/9.4/main/pg_hba.conf
RUN echo "listen_addresses='127.0.0.1'" >> /etc/postgresql/9.4/main/postgresql.conf
RUN echo "localhost vspyfe-db" >> /etc/hosts
#Define working directory.
WORKDIR /data
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/tmp", "/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

# Define commonly used JAVA_HOME variable
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
COPY ./entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT  ["/entrypoint.sh"]
CMD ["bash", "/usr/lib/postgresql/9.4/bin/postgres", "-D", "/var/lib/postgresql/9.4/main", "-c", "config_file=/etc/postgresql/9.4/main/postgresql.conf"]

and entrypoint.sh is :

#!/bin/sh
/etc/init.d/postgresql start
java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

I build with this command docker build --rm=true -t vspyfe/base:v1 . , and when I run the image with this command docker run -i -t vspyfe/base:v1 I got exception which says

Caused by: org.postgresql.util.PSQLException: FATAL: role "test" does not exist
at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:471)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:112)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)

Even if I try to create test db with default username and password (postgres, postgres), then I got exception says test db does not exist. I do not understand where I am doing wrong. In Dockerfile I specified each parameter that I used in application.properties file.

Have you checked your connection properties? I have a working example of a spring-boot app working nicely with postgres. Both are running in different containers. Please refer to:

Spring Boot + Postgres example, using the fabric8.io plugin

To run the example:

mvn clean package -DskipTests docker:stop docker:build docker:run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM