【问题标题】:Docker Postgres Connection issueDocker Postgres 连接问题
【发布时间】:2019-03-13 14:47:10
【问题描述】:

在尝试打开 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?

在我正在使用的 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

有人可以帮忙吗?

【问题讨论】:

标签: ruby-on-rails docker docker-compose dockerfile


【解决方案1】:

可能,您必须在 docker-compose 中定义 5432 端口。

    ports:      
     - 80:80
     - 5432:5432

【讨论】:

  • 我需要在 web: 或 db: 中添加 5432
【解决方案2】:

将此配置用于 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

【讨论】:

  • 好的,谢谢。我在database.yml中出错了。现在它工作正常了。现在我想将我的本地数据库表复制到docker postgreSql DB表中。
  • 它只创建了新表,不迁移现有表数据
  • postgresql 容器启动后,您应该运行脚本来迁移新表。或者将 .sql 脚本复制到容器并在 Dockerfile 中运行脚本。
猜你喜欢
  • 2020-08-26
  • 2021-06-08
  • 1970-01-01
  • 2011-09-21
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多