如何使用docker-compose连接到PostgreSQL?

如何使用docker-compose连接到PostgreSQL?

问题描述:

Want to use docker-compose to run api application and postgresql database together.

docker-compose file:

version: '3'

volumes:
  database_data:
    driver: local

services:
  db:
    image: postgres:latest
    volumes:
      - database_data:/var/lib/postgresql/data

  api:
    build: ./api
    expose:
      - 8080
    ports:
      - 8080:8080
    volumes:
      - ./api:/usr/src/app/
    links:
      - db
    environment:
      - PGHOST=db
      - PGDATABASE=postgres
      - PGUSER=postgres

Api main.go file:

func main() {
    db, err = gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres")
  // ...
}

When run the services, got message from log:

api_1     | [GIN] 2018/06/22 - 07:31:10 | 404 |      1.4404ms |      172.20.0.1 | GET      /posts
api_1     |
api_1     | (sql: database is closed)
api_1     | [2018-06-22 07:31:10]
api_1     |
api_1     | (sql: database is closed)
api_1     | [2018-06-22 07:31:10]
api_1     | [GIN] 2018/06/22 - 07:32:14 | 403 |        15.6µs |      172.20.0.1 | GET      /posts
db_1      | 2018-06-22 07:34:27.296 UTC [81] FATAL:  role "root" does not exist
db_1      | 2018-06-22 07:34:36.897 UTC [90] FATAL:  role "root" does not exist

Does this way not good? host=db in the connection string? Since db is the docker compose service name.


Add

It can work:

https://docs.docker.com/samples/library/postgres/#-or-via-psql

想要使用 docker-compose code>一起运行api应用程序和postgresql数据库。 p>

docker-compose code>文件: p>

 版本:'3'
 
卷:
 database_data  :
驱动程序:本地
 
服务:
 db:
图像:postgres:最新
卷:
-database_data:/ var / lib / postgresql / data 
 
 api:
 build:。  / api 
公开:
-8080 
端口:
-8080:8080 
卷:
-./api:/usr/src/app/
链接:
-db 
环境:  
-PGHOST = db 
-PGDATABASE = postgres 
-PGUSER = postgres 
  code>  pre> 
 
 

Api main.go code>文件: p>

  func main(){
 db,err = gorm.Open(“ postgres”,“ host = db port = 5432 user = postgres dbname = postgres”)
 //  ... 
} 
  code>  pre> 
 
 

运行服务时,从日志中获取消息: p>

  api_1 |  [GIN] 2018/06/22-07:31:10 |  404 |  1.4404毫秒|  172.20.0.1 |  GET / posts 
api_1 | 
api_1 |  (sql:数据库已关闭)
api_1 |  [2018-06-22 07:31:10] 
api_1 | 
api_1 |  (sql:数据库已关闭)
api_1 |  [2018-06-22 07:31:10] 
api_1 |  [GIN] 2018/06/22-07:32:14 |  403 |  15.6µs |  172.20.0.1 |  GET / posts 
db_1 |  2018-06-22 07:34:27.296 UTC [81]致命:角色“根”不存在
db_1 |  2018-06-22 07:34:36.897 UTC [90]致命:角色“根”不存在
  code>  pre> 
 
 

这种方式不好吗? 连接字符串中的 host = db code>? 由于 db code>是docker compose服务名称。 p>


添加 h1>

它可以正常工作: p>

https:/ /docs.docker.com/samples/library/postgres/#-or-via-psql p> div>

In the link you provided there are configuration settings that you did not added

restart: always
environment:
  POSTGRES_PASSWORD: example

You should try this

version: '3'

services:
  db:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: 'postgres'
    volumes:
      - database_data:/var/lib/postgresql/data

  api:
    build: ./api
    expose:
      - 8080
    ports:
      - 8080:8080
    volumes:
      - ./api:/usr/src/app/
    links:
      - db
    environment:
      - PGHOST: 'db'
      - PGDATABASE: 'postgres'
      - PGUSER: 'postgres'
      - PGPASSWORD: 'postgres'


volumes:
  database_data:
    driver: local

and

db, err := gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres password=postgres")