简体   繁体   English

Docker Postgres连接问题

[英]Docker Postgres Connection issue

I am facing below issue in docker with postgres while trying to open localhost:3000 尝试打开localhost:3000时,我在postgres中遇到以下问题在docker中

could not connect to server: Connection refused Is the server running on host 
"localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could 
 not connect to server: Cannot assign requested address Is the server running 
 on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Below the docker-compose file i am using 我正在使用的docker-compose文件下面

 version: '3'
 volumes:  
 postgres_data: {} 

 services:
 redis:
 image: redis
 command: redis-server
 ports:
  - "6379:6379"
app:    
build:      
  context: .      
  dockerfile: /Users/admin/git/generic/myapp/docker/app/Dockerfile 
depends_on:      
  - db  
ports:      
  - 3000:3000
db:    
image: postgres 
volumes:      
  - postgres_data:/var/lib/postgresql/data
web:    
build:      
  context: .      
  dockerfile: /Users/admin/git/generic/myapp/docker/web/Dockerfile  
depends_on:      
  - app    
ports:      
  - 80:80

Can anyone help regarding this? 有人可以帮忙吗?

Probably, you must to define 5432 port in your docker-compose. 可能您必须在docker-compose中定义5432端口。

    ports:      
     - 80:80
     - 5432:5432

Use this configuration for docker-compose.yml: 对docker-compose.yml使用以下配置:

version: '3.5'

  services:

    redis:
       image: redis
       command: redis-server
       ports:
         - "6379:6379"

    app:    
       build:      
         context: .      
         dockerfile: /Users/admin/git/generic/myapp/docker/app/Dockerfile 
       depends_on:      
         - db  
       ports:      
         - "3000:3000"  
       networks:
         services-network:
           aliases:
             - app

    web:    
      build:      
        context: .      
        dockerfile: /Users/admin/git/generic/myapp/docker/web/Dockerfile  
      depends_on:      
        - app    
      ports:      
        - "80:80"
      networks:
        services-network:
          aliases:
           - web
    db:
      image: postgres 
      volumes:      
       - postgres_data:/var/lib/postgresql/data
      environment:
       - POSTGRES_USER=postgres
       - POSTGRES_PASSWORD=postgres
       - POSTGRES_DB=db_name
      expose:
       - "5432"
      networks:
        services-network:
          aliases:
            - db

volumes:  
 postgres_data:

networks:
  services-network:
    name: services-network
    driver: bridge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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