2016-08-19 10 views
3

私はドッキング用のコンボでgoとpostgresのコンテナを接続しようとしています。ここでドッキングと一緒にgoとpostgresを接続する

は私のドッキングウィンドウ-compose.ymlです:まず

version: '2' 
services: 
    postgres: 
     image: postgres 
     ports: 
      - "5432" 
     environment: 
      - POSTGRES_PASSWORD=postgres 
    server: 
     build: ./server 
     command: gin 
     volumes: 
      - ./server:/go/src/app 
     ports: 
      - "8080:3000" 

、リンクがドッキングウィンドウ-compose.ymlにコマンドを使用して、私がしようとしていたが、それはサーバコンテナ内の任意のenv変数を作成していませんだから私はかなり混乱してしまった。 (それはSOMETHINGSOMETHING_PORT_5432_TCP_ADDRとSOMETHINGSOMETHING_5432_TCP_PORTを作成することになっています、それはないですか?)

それから私は、私はちょうどホストとしてhttp://postgresを使用することができますどこかで読んので、私はそれを試してみました。だから今、これは私のmain.goは(:私は「ORM」のGorpを使用しています。注):どのように見えるかです

func main() { 

    dbinfo := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable", 
     "postgres", 
     os.Getenv("DB_ENV_POSTGRES_PASSWORD"), 
     "http://postgres", 
     DB_NAME, 
    ) 

    db, err := sql.Open("postgres", dbinfo) 

    checkErr(err, "sql.Open failed") 


    // construct a gorp DbMap 
    dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}} 

    // add a table, setting the table name to 'posts' and 
    // specifying that the Id property is an auto incrementing PK 
    dbmap.AddTableWithName(Todo{}, "todos").SetKeys(true, "Id") 

    // create the table. in a production system you'd generally 
    // use a migration tool, or create the tables via scripts 
    err = dbmap.CreateTablesIfNotExists() 
    checkErr(err, "Create tables failed") 

    http.HandleFunc("/", handler) 
    http.ListenAndServe(":3001", nil) 
} 

ここでドッキングウィンドウ・作曲アップの詳細なログが(私はポート5432での接続を拒否します)です:

←[36mserver_1 |←[0m [gin] listening on port 3000 
←[33mpostgres_1 |←[0m The files belonging to this database system will be owned by user "postgres". 
←[33mpostgres_1 |←[0m This user must also own the server process. 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m The database cluster will be initialized with locale "en_US.utf8". 
←[33mpostgres_1 |←[0m The default database encoding has accordingly been set to "UTF8". 
←[33mpostgres_1 |←[0m The default text search configuration will be set to "english". 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m Data page checksums are disabled. 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m fixing permissions on existing directory /var/lib/postgresql/data ... ok 
←[33mpostgres_1 |←[0m creating subdirectories ... ok 
←[33mpostgres_1 |←[0m selecting default max_connections ... 100 
←[33mpostgres_1 |←[0m selecting default shared_buffers ... 128MB 
←[33mpostgres_1 |←[0m selecting dynamic shared memory implementation ... posix 
←[33mpostgres_1 |←[0m creating configuration files ... ok 
←[33mpostgres_1 |←[0m creating template1 database in /var/lib/postgresql/data/base/1 ... ok 
←[33mpostgres_1 |←[0m initializing pg_authid ... ok 
←[33mpostgres_1 |←[0m initializing dependencies ... ok 
←[33mpostgres_1 |←[0m creating system views ... ok 
←[33mpostgres_1 |←[0m loading system objects' descriptions ... ok 
←[33mpostgres_1 |←[0m creating collations ... ok 
←[33mpostgres_1 |←[0m creating conversions ... ok 
←[33mpostgres_1 |←[0m creating dictionaries ... ok 
←[33mpostgres_1 |←[0m setting privileges on built-in objects ... ok 
←[33mpostgres_1 |←[0m creating information schema ... ok 
←[33mpostgres_1 |←[0m loading PL/pgSQL server-side language ... ok 
←[33mpostgres_1 |←[0m vacuuming database template1 ... ok 
←[33mpostgres_1 |←[0m copying template1 to template0 ... ok 
←[33mpostgres_1 |←[0m copying template1 to postgres ... ok 
←[33mpostgres_1 |←[0m syncing data to disk ... ok 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m WARNING: enabling "trust" authentication for local connections 
←[33mpostgres_1 |←[0m You can change this by editing pg_hba.conf or using the option -A, or 
←[33mpostgres_1 |←[0m --auth-local and --auth-host, the next time you run initdb. 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m Success. You can now start the database server using: 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m  pg_ctl -D /var/lib/postgresql/data -l logfile start 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m waiting for server to start....LOG: database system was shut down at 2016-08-19 18:31:55 UTC 
←[33mpostgres_1 |←[0m LOG: MultiXact member wraparound protections are now enabled 
←[33mpostgres_1 |←[0m LOG: database system is ready to accept connections 
←[33mpostgres_1 |←[0m LOG: autovacuum launcher started 
←[33mpostgres_1 |←[0m done 
←[33mpostgres_1 |←[0m server started 
←[33mpostgres_1 |←[0m ALTER ROLE 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m LOG: received fast shutdown request 
←[33mpostgres_1 |←[0m LOG: aborting any active transactions 
←[33mpostgres_1 |←[0m LOG: autovacuum launcher shutting down 
←[33mpostgres_1 |←[0m LOG: shutting down 
←[33mpostgres_1 |←[0m waiting for server to shut down....LOG: database system is shut down 
←[33mpostgres_1 |←[0m done 
←[33mpostgres_1 |←[0m server stopped 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m PostgreSQL init process complete; ready for start up. 
←[33mpostgres_1 |←[0m 
←[33mpostgres_1 |←[0m LOG: database system was shut down at 2016-08-19 18:31:57 UTC 
←[33mpostgres_1 |←[0m LOG: MultiXact member wraparound protections are now enabled 
←[33mpostgres_1 |←[0m LOG: database system is ready to accept connections 
←[33mpostgres_1 |←[0m LOG: autovacuum launcher started 
←[36mserver_1 |←[0m 2016/08/19 18:32:05 Create tables failed dial tcp [::1]:5432: getsockopt: connection refused 
←[36mserver_1 |←[0m 2016/08/19 18:32:05 http: proxy error: dial tcp [::1]:3001: getsockopt: connection refused 
←[36mserver_1 |←[0m 2016/08/19 18:32:05 Create tables failed dial tcp [::1]:5432: getsockopt: connection refused 
←[36mserver_1 |←[0m 2016/08/19 18:32:05 http: proxy error: dial tcp [::1]:3001: getsockopt: connection refused 

私の質問はどうやってこれらのことを互いに話すことができるのでしょうか、ここで間違っていますか?

+0

試してみましょう 'sql.Open(" postgres "、" postgres:// postgres/yourdbname ")' – Plato

答えて

1

実際にはコンテナをリンクしていないので、os.Getenv( "DB_EN​​V_POSTGRES_PASSWORD")は空の文字列を生成していました。 docker-compose.ymlのサーバーコンテナにパスワード付きの環境変数を追加しました。これでデータベースに接続できます。

4

それはドッカーネットワークの質問です。それについてもっと読むことができますhere. Docker DNSはすべての努力をしていますので、あなたは作成ファイルに付けた名前で各コンテナにアクセスできます。

Posgres URLは通常、それはようなものになるだろう、だから、あなたのための

postgresql://user:[email protected]:port/database 

のように動作します。

db, err := sql.Open("postgres", "postgresql://user:[email protected]/mydatabase) 

postgresが標準ポート(5432)にある場合は、ポートは不要です。

+1

ありがとう、それは助けになりました:) – Nirri

+0

@Nirriこれを正解として受け入れることはできますか? – CESCO

関連する問題