My docker compose file is:
version: '3'
services:
postgres:
container_name: crm-psql
image: postgres:9.3
restart: always
environment:
- POSTGRES_DB=crm
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
volumes:
- ./db/postgres/data:/var/lib/postgresql/data
ports:
- 5432:5432
nginx:
container_name: crm-nginx
image: nginx:1.15
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
web:
build: .
ports:
- 9000:9000
volumes:
- .:/src/main/java
- ~/.ivy2:/root/.ivy2
- ~/.sbt:/root/.sbt
links:
- postgres
- nginx
The docker file for build is:
FROM openjdk:8
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $PATH:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
ENV SBT_VERSION 1.2.1
# Install curl
RUN \
apt-get update && \
apt-get -y install curl && \
apt-get -y install vim
# Install sbt
RUN \
curl -L -o sbt-$SBT_VERSION.deb https://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
dpkg -i sbt-$SBT_VERSION.deb && \
rm sbt-$SBT_VERSION.deb && \
apt-get update && \
apt-get -y install sbt
WORKDIR /src/main/java
COPY . /src/main/java
RUN sbt update
EXPOSE 9000
ENTRYPOINT ["sbt", "run"]
Whenever I do docker-compose up, the connection doesn't gets established to postgres. The exception is
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
However when i exec in container crm-psql and type psql crm, it goes into postgres shell. What is the issue here
I think your problem is that you use localhost to indicate your database. You should use your link instead. From the point of view of your "sbt" container, localhost is itself and not your local machine (with the port forwarding) or your postgresql container.
Generaly what I use is environment variables.
After you need to specify in your SBT script how to use your POSTGRES_SERVICE_HOST variable