简体   繁体   English

无法在Docker Ubuntu容器中允许端口

[英]Can't allow port in docker ubuntu container

Want to allow a port say 4000. Ran: 要允许一个端口说4000。

root@66548e7316fc:/# sudo ufw allow 4000/tcp
WARN: initcaps
[Errno 2] iptables v1.6.1: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.

Skipping adding existing rule
Skipping adding existing rule (v6)
root@66548e7316fc:/# 

root@66548e7316fc:/# sudo ufw status
ERROR: problem running iptables: iptables v1.6.1: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.


root@66548e7316fc:/# service ufw status
 * Firewall is not running...                                                                  [fail] 
root@66548e7316fc:/# sudo ufw reload
Firewall not enabled (skipping reload)
root@94d82c9ba002:/# sudo ufw enable
ERROR: problem running ufw-init
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 12
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 12
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'

Error occurred at line: 1
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
sysctl: setting key "net.ipv4.conf.all.accept_redirects": Read-only file system
sysctl: setting key "net.ipv4.conf.default.accept_redirects": Read-only file system
sysctl: setting key "net.ipv6.conf.all.accept_redirects": Read-only file system
sysctl: setting key "net.ipv6.conf.default.accept_redirects": Read-only file system
sysctl: setting key "net.ipv4.icmp_echo_ignore_broadcasts": Read-only file system
sysctl: setting key "net.ipv4.icmp_ignore_bogus_error_responses": Read-only file system
sysctl: setting key "net.ipv4.icmp_echo_ignore_all": Read-only file system
sysctl: setting key "net.ipv4.conf.all.log_martians": Read-only file system
sysctl: setting key "net.ipv4.conf.default.log_martians": Read-only file system

Problem loading ipv6 (skipping)
Problem running '/etc/ufw/before.rules'
Problem running '/etc/ufw/after.rules'
Problem running '/etc/ufw/user.rules'

root@94d82c9ba002:/# 
sudo ufw reload
Firewall not enabled (skipping reload)

I'm already root as you see root@66548e7316fc:/# in the above command prompt. 我已经是root root@66548e7316fc:/# ,在上述命令提示符中看到root@66548e7316fc:/#

For allowing a machine to connect to a container's port you should use the expose or ports Docker's property. 为了允许机器连接到容器的端口,您应该使用exposeports Docker的属性。

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE指令通知Docker运行时容器在指定的网络端口上进行侦听。 You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified. 您可以指定端口是侦听TCP还是UDP,如果未指定协议,则默认值为TCP。

from Docker's documentation . 从Docker 文档中获得

Or with ports : 或带ports

By default, when you create a container, it does not publish any of its ports to the outside world. 默认情况下,创建容器时,它不会将其任何端口发布到外界。 To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. 要使端口可用于Docker外部的服务或未连接到容器网络的Docker容器,请使用--publish或-p标志。

from here . 这里

Also you can specify an IP address on which this port will be listening: 您还可以指定此端口将在其上监听的IP地址:

docker run -p 127.0.0.1:4000:4000 my container 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM