【发布时间】:2020-01-06 04:36:50
【问题描述】:
我有两个不相互连接的容器: 1.我做了一个从dump.sql获取数据的图片postgres 这是 Dockerfile:
FROM postgres:11.1-alpine
COPY restore_db.sh /docker-entrypoint-initdb.d/
COPY db.sql /backup/
ENV PGDATA=/data
- 然后我用 docker run --name db -p 5432:5432 db 创建了容器
4.我用应用程序制作了一张图片。应用程序的 Dockerfile 如下所示:
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY build/libs/ /app/
# Make port 80 available to the world outside this container
EXPOSE 8085
# Define environment variable
ENV NAME app
# Run app when the container launches
CMD java -jar /app/olympic-0.0.1-SNAPSHOT.jar
- 我用run做了一个容器。
然后我使用 docker-compose up 文件看起来像:
version: '3'
services:
db:
image: db-data
container_name: postgres
ports:
- 5432:5432
volumes:
- ./pg_data:/data
environment:
POSTGRES_DB: innovation
POSTGRES_USER: postgres
PGDATA: /data
restart: always
web:
image: app
container_name: roc
environment:
POSTGRES_HOST: db
ports:
- 8085:8085
restart: always
links:
- db
```
here is property file:
```
spring.datasource.url=jdbc:postgresql://db:5432/innovation
spring.datasource.username=postgres
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.root=INFO
spring.output.ansi.enabled=ALWAYS
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.liquibase.change-log=classpath:liqubase/db.changelog-master.xml
spring.liquibase.url=jdbc:postgresql://db:5432/innovation
spring.liquibase.user=postgres
```
Thet are not able to be connected.
I always got an error:
org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
【问题讨论】:
-
看看你在哪里定义了
localhost:5432并将其更改为db:5432并确保你没有旧的图像容器,我建议你删除所有图像和容器并重新构建它们 -
@LinPy 没有帮助
-
你的数据库正在运行吗?
docker ps和docker logs DB_CONTINER? -
看看你的 2 个容器是否在同一个 docker 网络中。
-
你也可以试试 spring.datasource.url=jdbc:postgresql://postgres:5432/innovation
标签: postgresql docker docker-compose