postgresql Docker 和 Postgres:无法绑定 tcp 0.0.0.0:5432 地址已在使用中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38249434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:16:36  来源:igfitidea点击:

Docker & Postgres: Failed to bind tcp 0.0.0.0:5432 address already in use

macospostgresqldocker

提问by user3147424

Problem

问题

I'm trying to start postgres in a docker container on my Mac, but I keep getting the following error message

我正在尝试在 Mac 上的 docker 容器中启动 postgres,但我不断收到以下错误消息

docker: Error response from daemon: driver failed programming external connectivity on endpoint postgres (8392b9e5cfaa28f480fe1009dee461f97e82499726f4afc4e916358dd2d2f61e): Error starting userland proxy: Failed to bind tcp 0.0.0.0:5432 address already in use.

docker:来自守护进程的错误响应:驱动程序在端点 postgres 上编程外部连接失败(8392b9e5cfaa28f480fe1009dee461f97e82499726f4afc4e916358dd2d2f61e):启动用户区代理时出错:无法绑定使用 t.04.03 地址:04

I have postgres installed locally, but I stopped it and running

我在本地安装了 postgres,但我停止了它并运行

pg_ctl status

returns

回报

pg_ctl: no server running

pg_ctl:没有服务器运行

I've ran the following to check what's running on 5432

我运行了以下命令来检查 5432 上运行的内容

lsof -i tcp:5432

&

&

netstat -anp tcp | grep 5432

and nothing is running on the port.

端口上没有任何东西在运行。

Versions

版本

Mac - OS X El Capitan Version 10.11.2

Mac - OS X El Capitan 版本 10.11.2

PostgreSQL - 9.5

PostgreSQL - 9.5

Docker - Docker version 1.12.0-rc2, build 906eacd, experimental

Docker - Docker 版本 1.12.0-rc2,构建 906eacd,实验性

回答by blockloop

If you execute lsof -i :5432on the host you can see what process is bound to the port.

如果lsof -i :5432在主机上执行,您可以看到绑定到端口的进程。

Some instance of Postgres is running. You can execute kill <pid>to kill it if you want. You can also use 5432instead of 5432:5432in your docker command or docker-compose file and let docker choose the host port automatically.

Postgres 的某个实例正在运行。kill <pid>如果你愿意,你可以执行杀死它。您还可以在 docker 命令或 docker-compose 文件中使用5432代替,5432:5432让 docker 自动选择主机端口。

回答by Jamie

If lsof -i :5432doesn't show you any output, you can use sudo ss -lptn 'sport = :5432'to see what process is bound to the port.

如果lsof -i :5432没有显示任何输出,您可以使用sudo ss -lptn 'sport = :5432'查看绑定到端口的进程。

Proceed further with kill <pid>

继续进行 kill <pid>

回答by RtmY

In some cases it is critical to perform a more in-depth debugging to the problem before stopping or killing the container/process.

在某些情况下,在停止或终止容器/进程之前对问题执行更深入的调试至关重要。

Consider following the checklist below:

考虑遵循以下清单:

1) Check you current docker compose environment
Run docker-compose ps.
If port is in use by another container, stop it with docker-compose stop <service-name-in-compose-file>or remove it by replacing stopwith rm.

1)检查您当前的 docker compose 环境
Run docker-compose ps
如果 port 正在被另一个容器使用,请使用docker-compose stop <service-name-in-compose-file>将其停止或将其替换stoprm

2) Check the containers running outside your current workspace
Run docker psto see list of all containers running under your host.
If you find the port is in use by another container, you can stop it with docker stop <container-id>.
(*) Because you're not under the scope of the origin composeenvironment - it is a good practice first to use docker inspectto gather more information about the container that you're about to stop.

2)检查您的当前工作区外运行的容器
运行docker ps,看看你的主机下运行的所有容器的列表。
如果您发现该端口正在被另一个容器使用,您可以使用docker stop <container-id>.
(*) 因为您不在原始compose环境的范围内- 首先使用docker inspect收集有关您将要停止的容器的更多信息是一个很好的做法。

3) Check if port is used by other processes running on the host
For example if the port is 6379 run:

3)检查端口是否被主机上运行的其他进程使用
例如如果端口是6379运行:

$ sudo netstat -ltnp | grep ':6379'
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      915/redis-server 12 
tcp6       0      0 ::1:6379                :::*                    LISTEN      915/redis-server 12

(*) You can also use the lsofcommand which is mainly used to retrieve information about files that are opened by various processes (I suggest running netstatbefore that).

(*) 您还可以使用lsof命令,该命令主要用于检索有关各个进程打开的文件的信息(我建议在此netstat之前运行)。

So, In case of the output above the PIDis 915. Now you can run:

因此,如果输出高于PIDis 915。现在你可以运行:

$ ps j 915
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    1   915   915   915 ?           -1 Ssl    123   0:11 /usr/bin/redis-server 127.0.0.1:6379

And see the ID of the parent process (PPID) and the execution command.
You can also run: $ pstree -s <PID>to a visual display of the process and its related processes (install with: brew install pstree).

并查看父进程的 ID ( PPID) 和执行命令。
您还可以运行:$ pstree -s <PID>以可视化显示进程及其相关进程(安装方式为:)brew install pstree

In our case we can see that the process probably is a daemon (PPID is 1) - In that case consider running:
A) $ cat /proc/<PID>/statusin order to get a more in-depth information about the process like the number of threads spawned by the process, its capabilities, etc'.
B) $ systemctl status <PID>in order to see the systemdunit that caused the creation of a specific process. If the service is not critical - you can stop and disable the service.

在我们的例子中,我们可以看到该进程可能是一个守护进程(PPID 为 1) - 在这种情况下,请考虑运行:
A)$ cat /proc/<PID>/status以获得有关该进程的更深入信息,例如该进程产生的线程数,它的能力等'。
B)$ systemctl status <PID>为了查看导致创建特定进程的systemd单元。如果服务不是关键的 - 您可以停止并禁用该服务

4) Restart Docker service
Run sudo service docker restart.

4)重启Docker服务
运行sudo service docker restart

5) You reached this point and..
Only if its not placing your system at risk - consider restarting the server.

5)您已经达到了这一点并且..
仅当它不会使您的系统处于危险之中时 - 考虑重新启动服务器。