compose无法访问数据库端口

compose无法访问数据库端口

本文介绍了docker-compose无法访问数据库端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dockerfile:

FROM anapsix/alpine-java:8_server-jre_unlimited
# copy application
COPY server.jar /opt/test/
COPY application.yml /opt/test/

# expose server ports
EXPOSE 8080 18080

# Run via dumb-init
WORKDIR /opt/test
ENTRYPOINT ["java"]
CMD ["-jar", "server.jar"]

docker-compose文件:

services:
  backend-server:
    image: test.com/server:latest
    build: .
    depends_on:
      - database-server
    ports:
        - "127.0.0.1:8080:8080"
        - "127.0.0.1:18080:18080"
  database-server:
    image: postgres:9.6
    ports:
        - "127.0.0.1:5432:5432"
    environment:
      - POSTGRES_PASSWORD testtest

应用程序属性文件:

spring:
  datasource:
    # use default user/database created by PostgreSQL Docker image upon startup
    url: jdbc:postgresql://localhost/postgres
    username: postgres
    password: testtest
    driver-class-name: org.postgresql.Driver

当我运行docker-compose时,它将创建一个数据库容器和一个连接到该数据库容器的应用程序容器.

When I run the docker-compose up, it will create a database container and an application container which connects to the database container.

但是spring应用程序容器在启动过程中遇到了一些错误:

But the spring application container encounters some problem during bootup with error:

docker container ls 显示数据库正在运行,如果直接运行spring应用程序,则可以访问它.

docker container ls shows the database is running, and I can access it if I run the spring application directly.

a295bfd16e2c        postgres:9.6        "docker-entrypoint.s…"   About a minute ago   Up About a minute   127.0.0.1:5432->5432/tcp   server_database-server_1

那我搞砸了会是什么问题?

So what would be the problem that I messed up?

我找到的解决方案

services:
  backend-server:
    image: test.com/server:latest
    build: .
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://database-server/postgres
    depends_on:
      - database-server
    ports:
        - 8080:8080
        - 18080:18080
  database-server:
    image: postgres:9.6
    ports:
        - 5432:5432
    environment:
      - POSTGRES_PASSWORD testtest

但是我仍然无法从计算机的 localhost:8080 访问应用程序,即使容器的日志显示为

But I still cannot access the application from localhost:8080 of my computer, even the log of the container says

2018-10-31 15:02:40.881信息1 --- [主要]com.test.server.Application:在以下位置启动的应用程序12.823秒(JVM运行13.235)

2018-10-31 15:02:40.881 INFO 1 --- [ main] com.test.server.Application : Started Application in 12.823 seconds (JVM running for 13.235)

推荐答案

services:
  backend-server:
    image: test.com/server:latest
    build: .
    depends_on:
      - database-server
    ports:
        - "127.0.0.1:8080:8080" #change to 8080:8080 without ""and by default set to local so you dont need add local ip
        - "127.0.0.1:18080:18080"  #change to 18080:18080 without ""and by default set to local so you dont need add local ip
  database-server:
    image: postgres:9.6
    ports:
        - "127.0.0.1:5432:5432" #change to 5432:5432 without ""and by default set to local so you dont need add local ip
    environment:
      - POSTGRES_PASSWORD testtest

这篇关于docker-compose无法访问数据库端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:45