[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eda68d54e997 training/webapp "python app.py" 3 minutes ago Up 2 minutes 0.0.0.0:1026->5000/tcp blissful_goldstine
0e044f323370 training/webapp "python app.py" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]#
[root@huixuan ~]# docker run -d -p 5000:5000 training/webapp python app.py
56af280bb2b399399a9a40a5fa52ac0d7e787cca7ac2446ccbbb3faecccde935
/usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint laughing_heisenberg (b3ee694cd9c6bb3f7c8d7129aca3bf99b5eabc023daab0d08a5e78358d23b08e): Bind for 0.0.0.0:5000 failed: port is already allocated.
[root@huixuan ~]# docker stop 0e044f323370
0e044f323370
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eda68d54e997 training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:1026->5000/tcp blissful_goldstine
[root@huixuan ~]# docker run -d -p 5000:5000 training/webapp python app.py
fcced8f54a68432cb652a7b755047160869ff9110cd6ec2b850f14a666a72b28
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcced8f54a68 training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp kickass_pare
eda68d54e997 training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:1026->5000/tcp blissful_goldstine
[root@huixuan ~]#
另外,我们可以指定容器绑定的网络地址,比如绑定127.0.0.1。
[root@huixuan ~]# docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
eda9d7e0083933213dff616a38e39975c89783f018ae9554c87493cc2028e247
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eda9d7e00839 training/webapp "python app.py" 3 seconds ago Up 2 seconds 127.0.0.1:5001->5000/tcp mystifying_haibt
fcced8f54a68 training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp kickass_pare
eda68d54e997 training/webapp "python app.py" 6 minutes ago Up 6 minutes 0.0.0.0:1026->5000/tcp blissful_goldstine
[root@huixuan ~]#
[root@huixuan ~]# docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
2eaa281d655cf95a2df079b8488e0e5cc64850e610ad522ba14a0e2ba9ca2e02
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2eaa281d655c training/webapp "python app.py" 2 seconds ago Up 1 second 5000/tcp, 127.0.0.1:5000->5000/udp nifty_swirles
eda9d7e00839 training/webapp "python app.py" About a minute ago Up About a minute 127.0.0.1:5001->5000/tcp mystifying_haibt
fcced8f54a68 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp kickass_pare
eda68d54e997 training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:1026->5000/tcp blissful_goldstine
[root@huixuan ~]#
docker port 命令可以让我们快捷地查看端口的绑定情况。
[root@huixuan ~]# docker port 2eaa281d655c
5000/udp -> 127.0.0.1:5000
[root@huixuan ~]# docker port eda9d7e00839
5000/tcp -> 127.0.0.1:5001
[root@huixuan ~]#