首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"docker run“的-P选项实际上是做什么的?

"docker run“的-P选项实际上是做什么的?
EN

Stack Overflow用户
提问于 2020-07-22 21:44:40
回答 1查看 472关注 0票数 2

我为我们的团队设置了一个带有docker的pypiserver,遇到了一个问题,包的发布不起作用。仔细阅读本教程后,我发现在我的docker run ...命令末尾缺少-P .htpasswd packages

与pypiserver文档( docker部分中的最后一个命令)进行比较:https://pypi.org/project/pypiserver/#using-the-docker-image

代码语言:javascript
复制
docker run -p 80:8080 -v ~/.htpasswd:/data/.htpasswd pypiserver/pypiserver:latest -P .htpasswd packages

根据man docker run的说法,-P选项应该只接收false或true值(不是文件列表,甚至不是单个文件),并且它将容器的端口映射到主机的随机端口,这在我的用例中显然不会发生,因为docker port containername只输出我用小写-p选项配置的单个端口映射。

那么这里到底发生了什么呢?我首先想到的是,也许文件列表与-P选项没有任何关系(也许它只是一个切换,如果它出现在命令中,它会自动设置为true ),但是当我删除文件列表时,我得到了错误:

代码语言:javascript
复制
> docker run -p 80:8080 -v ~/.htpasswd:/data/.htpasswd pypiserver/pypiserver:latest -P
usage error: option -P requires argument

要么是我严重误解了CLI接口,要么是-P做了一些不同的事情,如dockers手册页所述。

代码语言:javascript
复制
-P, --publish-all=true|false
          Publish all exposed ports to random ports on the host interfaces. The default is false.

       When set to true publish all exposed ports to the host interfaces. The default is false. If the operator uses -P (or -p) then Docker will make the exposed port accessible on the host and the ports will be available to any client that can reach the host.
       When using -P, Docker will bind any exposed port to a random port on the host within an ephemeral port range defined by /proc/sys/net/ipv4/ip_local_port_range. To find the mapping between the host ports and the exposed ports, use docker port(1).
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-22 21:54:28

你找错地方了。是的,要执行docker run操作,-P选项会将所有暴露的端口发布到主机上随机编号较高的端口。但是,在此之前,docker run命令本身是顺序敏感的,并且需要在命令行的右侧传递docker run的标志:

代码语言:javascript
复制
docker run ${args_to_run} ${image_name} ${cmd_override}

换句话说,docker一旦发现不是arg to run的内容,就会将下一个内容解析为图像名称,然后其余的arg将成为容器内CMD的新值。

接下来,当您在容器中定义了一个入口点时,该入口点将与CMD值连接在一起,以形成一个在容器内运行的命令。例如,如果入口点是/entrypoint.sh,并且你用-P filename覆盖了CMD,那么docker将运行/entrypoint.sh -P filename来启动你的容器。

因此,您需要查看pypiserver image docs,以了解其入口点所需的语法:

代码语言:javascript
复制
 -P, --passwords PASSWORD_FILE

 Use apache htpasswd file PASSWORD_FILE to set usernames & passwords when
 authenticating certain actions (see -a option).
 To allow unauthorized access, use:
   -P . -a .

您还可以深入他们的存储库,查看他们的set the entrypoint to:

代码语言:javascript
复制
ENTRYPOINT ["pypi-server", "-p", "8080"]

因此,使用-P .htpasswd packages时,容器内的命令为:

代码语言:javascript
复制
pypi-server -p 8080 -P .htpasswd packages
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63035651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档