簡體   English   中英

用docker-compose連接go和postgres

[英]Connecting go and postgres with docker-compose

我正在嘗試使用docker-compose連接go和postgres容器。

這是我的docker-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"

起初我嘗試使用docker-compose.yml中的links命令,但這並沒有在服務器容器中創建任何env變量,所以這讓我很困惑。 (它應該創建SOMETHINGSOMETHING_PORT_5432_TCP_ADDR和SOMETHINGSOMETHING_5432_TCP_PORT,不是嗎?)

然后我讀到某個地方,我可以使用http:// postgres作為主機,所以我嘗試了。 所以現在這就是我的main.go的樣子(注意:我正在使用Gorp作為“ORM”):

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)
}

這是docker-compose的詳細日志(我在端口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

所以我的問題是如何讓這些能夠彼此交談以及我在這里做錯了什么?

這是一個碼頭網絡問題。你可以在這里閱讀更多相關信息 Docker dns可以完成所有艱苦的工作,因此您可以通過compose文件中的名稱訪問每個容器。

Posgres網址通常像

postgresql://user:password@ip:port/database

所以對你來說,就像是。

db, err := sql.Open("postgres", "postgresql://user:password@postgres/mydatabase)

請注意,如果postgres位於標准端口(5432)上,則不需要該端口。

事實證明,因為我實際上沒有鏈接容器,os.Getenv(“DB_EN​​V_POSTGRES_PASSWORD”)正在生成一個空字符串。 在docker-compose.yml中為服務器容器添加了一個帶密碼的環境變量,現在我可以連接到我的數據庫了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM