docker - "docker run"的 -P 选项实际上有什么作用?

标签 docker command-line-interface

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

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

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),但是当我删除文件列表我收到错误:

> docker run -p 80:8080 -v ~/.htpasswd:/data/.htpasswd pypiserver/pypiserver:latest -P
usage error: option -P requires argument

要么是我严重误解了 CLI 接口(interface),要么是 -P 做了一些与 docker 手册页中描述的不同的事情。

-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).


最佳答案

您找错地方了。是的,发送至docker run -P选项会将所有公开的端口发布到主机上的随机高编号端口。然而,在您开始之前,docker run命令本身是顺序敏感的,并且标记为 docker run需要在命令行的右侧部分传递:

docker run ${args_to_run} ${image_name} ${cmd_override}

换句话说,只要 docker 看到不是 run 参数的东西,它将下一个内容解析为图像名称,然后其余参数成为 CMD 的新值在容器内。

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

因此您需要查看pypiserver image docs查看它们的入口点需要什么语法:

 -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至:

ENTRYPOINT ["pypi-server", "-p", "8080"]

所以-P .htpasswd packages容器内的命令变为:

pypi-server -p 8080 -P .htpasswd packages

关于docker - "docker run"的 -P 选项实际上有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63035651/

相关文章:

php - 长时间运行的 php 脚本的内存注意事项

node.js - 在计算机上本地存储 JWT token 的位置?

python - standard_init_linux.go :211:exec user process caused "no such file or directory" with alpine linux and python

docker - docker容器什么时候停止?

php - 第一个请求后PHP开发服务器崩溃

php - 使用 Zend Framework 创建 cronjob

javascript - 无法在 linux 中运行 npx react-native init

.net - 无法连接到由 docker compose 启动的 .net 容器

linux - docker - 写入管道 : bad file descriptor

Docker 容器未开始提供 "OCI runtime create failed"