You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker无法绑定指定地址:提示地址已占用但实际未占用

解决Docker端口绑定报错:address already in use

Hey there, let's work through this port binding error you're hitting. The error listen tcp 172.19.5.1:22: bind: address already in use tells us that something on your host is already using port 22 on the 172.19.5.1 IP address you're trying to bind to. Here's how to fix it step by step:

第一步:找出占用端口的进程

First, let's pinpoint exactly which process is holding onto that port. Run either of these commands in your host terminal:

# 使用ss命令(轻量且默认多数系统已安装)
ss -tulpn | grep "172.19.5.1:22"

# 或者使用lsof(若未安装需先执行 sudo apt install lsof / sudo yum install lsof)
lsof -i :22@172.19.5.1

These commands will show you the PID and name of the process using the port. The most common culprit here is your host's own SSH daemon (sshd), since it typically listens on port 22 across all available IPs.

第二步:根据占用进程选择解决方法

情况1:主机sshd占用了该端口

If the process is sshd, it means your host's SSH service is configured to listen on all available IPs (including 172.19.5.1). To fix this, restrict sshd to only listen on specific IPs you need:

  1. Edit the sshd configuration file:
sudo nano /etc/ssh/sshd_config
  1. Find the ListenAddress line (it might be commented out with a #). Uncomment it and set it to the IPs you want sshd to use (exclude 172.19.5.1). For example:
ListenAddress 192.168.1.100  # 替换成你的主机其他公网/内网IP
ListenAddress 127.0.0.1
  1. Restart the sshd service to apply changes:
sudo systemctl restart sshd

情况2:其他进程/容器占用了该端口

If it's another process or Docker container using the port, you have two straightforward options:

  • Stop the conflicting process/container: Use sudo kill <PID> for a regular process, or sudo docker stop <container-name> for a Docker container.
  • Change the host port in your Docker run command: For example, bind to port 2222 instead of 22 to avoid the conflict:
sudo docker run --privileged -d -p 172.19.5.1:2222:22 -p 172.19.5.1:5432:5432 --name test --hostname test -v /sys/fs/cgroup:/sys/fs/cgroup:ro --net cdnet image_name

额外检查:确认IP是否绑定到主机网卡

One more thing to verify: Make sure the IP 172.19.5.1 is actually assigned to one of your host's network interfaces. Run:

ip addr show

If you don't see 172.19.5.1 listed, add it to your desired interface (replace eth0 with your actual network interface name, like ens33 or wlan0):

sudo ip addr add 172.19.5.1/24 dev eth0

Once you've fixed the port conflict or IP assignment issue, re-run your Docker command and it should work as expected.

内容的提问来源于stack exchange,提问作者ocket8888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:13:16