<p>Hi everyone!</p>
<p>Golang container isn't finding Postgres db's. I have one idea but can't implement this logic. Is it possible to retry to connect to database, if connection is refused?
for example...</p>
<p><a href="https://forum.golangbridge.org/t/golang-not-connecting-to-postgres-thought-gorm-and-hosted-by-docker/6571" rel="nofollow">And this is a link to my topic in golang forum, reddit code markdown works bad.</a></p>
<pre><code>func Init() {
authConnect := fmt.Sprintf( "host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
os.Getenv( "AUTH_DB_HOST" ),
os.Getenv( "AUTH_DB_PORT" ),
os.Getenv( "AUTH_DB_USER" ),
os.Getenv( "AUTH_DB_NAME" ),
os.Getenv( "AUTH_DB_PASSWORD" ) )
AuthConnect:
AuthDB, err = gorm.Open( "postgres", authConnect )
if err != nil {
log.Printf("Got error when connect database `auth`, the error is '%v'", err)
goto AuthConnect
}
userConnect := fmt.Sprintf( "host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
os.Getenv( "USER_DB_HOST" ),
os.Getenv( "USER_DB_PORT" ),
os.Getenv( "USER_DB_USER" ),
os.Getenv( "USER_DB_NAME" ),
os.Getenv( "USER_DB_PASSWORD" ) )
UserConnect:
UserDB, err = gorm.Open("postgres", userConnect )
if err != nil {
log.Fatalf("Got error when connect database `user`, the error is '%v'", err)
goto UserConnect
}
}
</code></pre>
<p>If you have any suggestions please help me.</p>
<p>Here are my code, logs and other details.</p>
<pre><code>func Init() {
authConnect := fmt.Sprintf( "host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
os.Getenv( "AUTH_DB_HOST" ),
os.Getenv( "AUTH_DB_PORT" ),
os.Getenv( "AUTH_DB_USER" ),
os.Getenv( "AUTH_DB_NAME" ),
os.Getenv( "AUTH_DB_PASSWORD" ) )
AuthDB, err = gorm.Open( "postgres", authConnect )
if err != nil {
log.Fatalf("Got error when connect database `auth`, the error is '%v'", err)
}
userConnect := fmt.Sprintf( "host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
os.Getenv( "USER_DB_HOST" ),
os.Getenv( "USER_DB_PORT" ),
os.Getenv( "USER_DB_USER" ),
os.Getenv( "USER_DB_NAME" ),
os.Getenv( "USER_DB_PASSWORD" ) )
UserDB, err = gorm.Open("postgres", userConnect )
if err != nil {
log.Fatalf("Got error when connect database `user`, the error is '%v'", err)
}
}
</code></pre>
<p>After starting the container I get this message.</p>
<pre><code>auth_1 | 2017/09/07 12:04:32 Got error when connect database `auth`, the error is 'dial tcp 127.0.0.1:5437: getsockopt: connection refused'
</code></pre>
<p>My docker-compose.yml</p>
<pre><code>version: '3'
</code></pre>
<p>services:
user_db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRESS_PASSWORD=postgres
- POSTGRES_DB=user
ports:
- "5432:5432"
auth_db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRESS_PASSWORD=postgres
- POSTGRES_DB=auth
ports:
- "5437:5432"
auth:
build:
context: .
dockerfile: ./auth/Dockerfile
environment:
- AUTOMIGRATE=true
- AUTH_DB_HOST=localhost
- USER_DB_HOST=localhost
- AUTH_DB_PORT=5437
- USER_DB_PORT=5432
- AUTH_DB_USER=postgres
- USER_DB_USER=postgres
- AUTH_DB_NAME=auth
- USER_DB_NAME=user
- AUTH_DB_PASSWORD=postgres
- USER_DB_PASSWORD=postgres
ports:
- "8070:8070"
depends_on:
- user_db
- auth_db
user:
build:
context: .
dockerfile: ./user/Dockerfile
environment:
- AUTOMIGRATE=true
ports:
- "8090:8090"
depends_on:
- user_db</p>
<p>My docker-compose logs</p>
<pre><code> Attaching to grsuser_auth_db_1, grsuser_user_db_1, grsuser_user_1, grsuser_auth_1
</code></pre>
<p>auth_db_1 | The files belonging to this database system will be owned by user "postgres".
auth_db_1 | This user must also own the server process.
auth_db_1 |
auth_db_1 | The database cluster will be initialized with locale "en_US.utf8".
auth_db_1 | The default database encoding has accordingly been set to "UTF8".
auth_db_1 | The default text search configuration will be set to "english".
auth_1 | 2017/09/07 12:04:32 Got error when connect database <code>auth</code>, the error is 'dial tcp 127.0.0.1:5437: getsockopt: connection refused'
auth_db_1 |
auth_db_1 | Data page checksums are disabled.
user_db_1 | The files belonging to this database system will be owned by user "postgres".
auth_db_1 |
user_db_1 | This user must also own the server process.
user_db_1 |
user_db_1 | The database cluster will be initialized with locale "en_US.utf8".
auth_db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
auth_db_1 | creating subdirectories ... ok
user_db_1 | The default database encoding has accordingly been set to "UTF8".
auth_db_1 | selecting default max_connections ... 100
user_db_1 | The default text search configuration will be set to "english".
auth_db_1 | selecting default shared_buffers ... 128MB
user_db_1 |
user_db_1 | Data page checksums are disabled.
auth_db_1 | selecting dynamic shared memory implementation ... posix
auth_db_1 | creating configuration files ... ok
user_db_1 |
auth_db_1 | running bootstrap script ... ok
user_db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
user_1 | Now listening on: http://localhost:8090
auth_db_1 | performing post-bootstrap initialization ... ok
user_db_1 | creating subdirectories ... ok
user_db_1 | selecting default max_connections ... 100
auth_db_1 |
user_1 | Application started. Press CTRL+C to shut down.
auth_db_1 | WARNING: enabling "trust" authentication for local connections
user_db_1 | selecting default shared_buffers ... 128MB
auth_db_1 | You can change this by editing pg_hba.conf or using the option -A, or
user_1 | Now listening on: http://localhost:8090
user_db_1 | selecting dynamic shared memory implementation ... posix
auth_db_1 | --auth-local and --auth-host, the next time you run initdb.
user_1 | Application started. Press CTRL+C to shut down.
user_db_1 | creating configuration files ... ok
auth_db_1 | syncing data to disk ... ok
user_db_1 | running bootstrap script ... ok
auth_db_1 |
auth_db_1 | Success. You can now start the database server using:
user_db_1 | performing post-bootstrap initialization ... ok
user_db_1 | syncing data to disk ... ok
user_db_1 |
user_db_1 | Success. You can now start the database server using:
user_db_1 |
user_db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
auth_db_1 |
user_db_1 |
auth_db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
user_db_1 |
user_db_1 | WARNING: enabling "trust" authentication for local connections
auth_db_1 |
user_db_1 | You can change this by editing pg_hba.conf or using the option -A, or
auth_db_1 | ****************************************************
user_db_1 | --auth-local and --auth-host, the next time you run initdb.
auth_db_1 | WARNING: No password has been set for the database.
auth_db_1 | This will allow anyone with access to the
user_db_1 | ****************************************************
auth_db_1 | Postgres port to access your database. In
user_db_1 | WARNING: No password has been set for the database.
auth_db_1 | Docker's default configuration, this is
user_db_1 | This will allow anyone with access to the
auth_db_1 | effectively any other container on the same
user_db_1 | Postgres port to access your database. In
auth_db_1 | system.
auth_db_1 |
auth_db_1 | Use "-e POSTGRES_PASSWORD=password" to set
user_db_1 | Docker's default configuration, this is
auth_db_1 | it in "docker run".
user_db_1 | effectively any other container on the same
auth_db_1 | ****************************************************
user_db_1 | system.
auth_db_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address
auth_db_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
auth_db_1 | LOG: database system was shut down at 2017-09-07 11:55:29 UTC
auth_db_1 | LOG: MultiXact member wraparound protections are now enabled
auth_db_1 | LOG: database system is ready to accept connections
auth_db_1 | LOG: autovacuum launcher started
auth_db_1 | done
auth_db_1 | server started
auth_db_1 | CREATE DATABASE
auth_db_1 |
auth_db_1 | ALTER ROLE
auth_db_1 |
auth_db_1 |
user_db_1 |
user_db_1 | Use "-e POSTGRES_PASSWORD=password" to set
user_db_1 | it in "docker run".
user_db_1 | ****************************************************
user_db_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address
user_db_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
user_db_1 | LOG: database system was shut down at 2017-09-07 11:55:29 UTC
user_db_1 | LOG: MultiXact member wraparound protections are now enabled
user_db_1 | LOG: database system is ready to accept connections
user_db_1 | LOG: autovacuum launcher started
user_db_1 | done
auth_db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
auth_db_1 |
auth_db_1 | LOG: received fast shutdown request
user_db_1 | server started
user_db_1 | CREATE DATABASE
user_db_1 |
user_db_1 | ALTER ROLE
user_db_1 |
user_db_1 |
user_db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
user_db_1 |
user_db_1 | LOG: received fast shutdown request
auth_db_1 | waiting for server to shut down....LOG: aborting any active transactions
auth_db_1 | LOG: autovacuum launcher shutting down
auth_db_1 | LOG: shutting down
auth_db_1 | LOG: database system is shut down
auth_db_1 | done
auth_db_1 | server stopped
auth_db_1 |
auth_db_1 | PostgreSQL init process complete; ready for start up.
auth_db_1 |
auth_db_1 | LOG: database system was shut down at 2017-09-07 11:55:34 UTC
auth_db_1 | LOG: MultiXact member wraparound protections are now enabled
auth_db_1 | LOG: database system is ready to accept connections
auth_db_1 | LOG: autovacuum launcher started
auth_db_1 | LOG: received smart shutdown request
auth_db_1 | LOG: autovacuum launcher shutting down
user_db_1 | waiting for server to shut down....LOG: aborting any active transactions
user_db_1 | LOG: autovacuum launcher shutting down
user_db_1 | LOG: shutting down
user_db_1 | LOG: database system is shut down
user_db_1 | done
user_db_1 | server stopped
user_db_1 |
user_db_1 | PostgreSQL init process complete; ready for start up.
user_db_1 |
user_db_1 | LOG: database system was shut down at 2017-09-07 11:55:34 UTC
user_db_1 | LOG: MultiXact member wraparound protections are now enabled
user_db_1 | LOG: autovacuum launcher started
user_db_1 | LOG: database system is ready to accept connections
auth_db_1 | LOG: shutting down
auth_db_1 | LOG: database system is shut down
auth_db_1 | LOG: database system was shut down at 2017-09-07 12:00:31 UTC
auth_db_1 | LOG: MultiXact member wraparound protections are now enabled
auth_db_1 | LOG: database system is ready to accept connections
auth_db_1 | LOG: autovacuum launcher started
grsuser_auth_1 exited with code 1</p>
<p>Regards, Khachatur!</p>
<hr/>**评论:**<br/><br/>michele: <pre><p>Formatting is a bit of a mess, but it looks to me like you got networking wrong: you don't need the <code>ports</code> directives for the two dbs. The <code>auth</code> container will be able to connect to the dbs because of the <code>depends_on</code> directive. Plus, you don't connect to <code>localhost</code>, but use the containers' names instead.</p>
<p>So remove <code>ports</code> from the dbs and change <code>AUTH_DB_PORT</code> to <code>5432</code>, <code>AUTH_DB_HOST</code> to <code>auth_db</code> and <code>USER_DB_HOST</code> to <code>user_db</code>.</p>
<p>Should work, let me know if it doesn't.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传