Docker 容器启动时端口映射失败

介绍

在一台虚拟机上创建容器时因为端口映射的问题而导致容器启动失败,并提示 docker: Error response from daemon: driver failed programming external connectivity on endpoint orderer.example.com (ae62c5d74521cc7ea21dc4d4c762cf09390839a1a21d8dcfdcb3784ecdc5e568): Bind for 0.0.0.0:7050 failed: port is already allocated.


1. 错误发现过程

  • 通过 docker-compose 启动一个容器是提示无法启动容器,错误原因是 Bind for 0.0.0.0:7050 failed: port is already allocated'

    1
    2
    3
    4
    5
    6
    7
    $ docker-compose -f docker-compose-orderer.yaml up -d
    Creating orderer.example.com ... error

    ERROR: for orderer.example.com Cannot start service orderer.example.com: b'driver failed programming external connectivity on endpoint orderer.example.com (b1253c6e3542219f989fb9f6508c738066aeeb2fcdebd1e13b9b85c63c2715dd): Bind for 0.0.0.0:7050 failed: port is already allocated'

    ERROR: for orderer.example.com Cannot start service orderer.example.com: b'driver failed programming external connectivity on endpoint orderer.example.com (b1253c6e3542219f989fb9f6508c738066aeeb2fcdebd1e13b9b85c63c2715dd): Bind for 0.0.0.0:7050 failed: port is already allocated'
    ERROR: Encountered errors while bringing up the project.
  • 接着直接使用 docker run 命令启动容器,还是提示一样的错误。

    1
    2
    3
    $ docker run --name orderer.example.com -d -p 7050:7050 hyperledger/fabric-orderer:x86_64-1.1.0 
    07c97104c290f470588bf0cfe041f76771bfc8586b3ad0fe784a20f97c4e4a6f
    docker: Error response from daemon: driver failed programming external connectivity on endpoint orderer.example.com (ae62c5d74521cc7ea21dc4d4c762cf09390839a1a21d8dcfdcb3784ecdc5e568): Bind for 0.0.0.0:7050 failed: port is already allocated.
  • 查看主机的端口是否被占用:

    1
    2
    3
    4
    5
    6
    7
    8
    $ netstat -ntlp
    (Not all processes could be identified, non-owned process info
    will not be shown, you would have to be root to see it all.)
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
    tcp6 0 0 :::22 :::* LISTEN -
    tcp6 0 0 :::2375 :::* LISTEN -

    发现并没有 7050 端口并没有在使用。

  • 启动另一个容器,绑定主机的另一个端口:

    1
    2
    $ docker run -d -p 8050:7050 hyperledger/fabric-orderer:x86_64-1.1.0 
    fba31d37ff4cc409740ce8cd045f4f4bc6a76f7c69c454a2d7e380327613acb0
    1
    2
    3
    $ docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    fba31d37ff4c hyperledger/fabric-orderer:x86_64-1.1.0 "orderer" 8 seconds ago Up 7 seconds 0.0.0.0:8050->7050/tcp confident_wescoff

    可以启动。

  • 查看 docker 服务状态:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ systemctl status docker
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/docker.service.d
    └─override.conf
    Active: active (running) since Wed 2018-12-05 09:29:11 CST; 1h 14min ago
    Docs: https://docs.docker.com
    Main PID: 987 (dockerd)
    Tasks: 35
    Memory: 161.2M
    CPU: 54.874s
    CGroup: /system.slice/docker.service
    ├─ 987 /usr/bin/dockerd
    └─1089 docker-containerd --config /var/run/docker/containerd/containerd.toml

    服务显示是正常运行的。


2.解决方法

网上查看了一些方法,可以通过重启 docker 服务来解决:

1
$ sudo systemctl restart docker

真的解决了。

1
2
3
4
5
6
$ docker run --name orderer.example.com -d -p 7050:7050 hyperledger/fabric-orderer:x86_64-1.1.0 
2f289e5225344e4e07230a0985e9f51f2ef6584af263be28388bdb7f6c80af35

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f289e522534 hyperledger/fabric-orderer:x86_64-1.1.0 "orderer" 7 seconds ago Up 6 seconds 0.0.0.0:7050->7050/tcp orderer.example.com