Unable to connect to database Postgres Docker container from other container

WBOY
Release: 2024-02-09 10:30:08
forward
1060 people have browsed it

无法从其他容器连接到数据库 Postgres Docker 容器

php Editor Youzi encountered a problem when using a Docker container to connect to the Postgres database: unable to connect to the Postgres Docker container from other containers. This issue can cause blockages during development, so a workaround needs to be found. In this article, we will explore the cause of this problem and provide some possible solutions to help solve the problem of not being able to connect to the Postgres Docker container.

Question content

I have a service in a Docker container that syncs data from another database to my database, but the sync container cannot connect to the Postgres DB container.

container

My error message

Cannot connect to the database, exiting now: dial tcp 127.0.0.1:5432: connect: connection refused"
Copy after login

I can connect to the database via TablePlus on 127.0.0.1:5432, but via Docker Inspect is

The sync service is written in Go - the database name, user and password are the same as the one I'm connecting to via TablePlus. I also tried IP 0.0.0.0 and 172.19.0.2.

Does anyone know why it won't connect?

Solution

Try to connect to postgres:5432

You need to use the container name when connecting from another docker container

The above is the detailed content of Unable to connect to database Postgres Docker container from other container. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!