<p>您可以将最后一个语句,即<code>CMD</code>更改为下面的语句。在</p>
<p>在下面的语句中指定pip位置的绝对路径</p>
<pre><code>CMD ["/usr/bin/pip", "install", "-r", "/root/sourceCode/requirements.txt"]
</code></pre>
<p><strong>更新:</strong>根据评论添加其他答案。在</p>
<p>必须注意的一点是,如果定制的映像需要附加的需求,那么这应该是映像的一部分,而不是在运行时执行。在</p>
<p>使用下面的基本图像进行测试:</p>
^{pr2}$
<p>因此,安装包应该使用Dockerfile的<code>RUN</code>命令来运行。<br/>
并且<code>CMD</code>应该被用来作为容器内的进程运行的应用程序。在</p>
<p>只需通过运行下面的命令检查基本映像是否有任何pip包,结果是<strong>什么都没有</strong>。在</p>
<pre><code>docker run rm name=testpy colstrom/python:legacy /usr/bin/pip freeze
</code></pre>
<p>这个例子很简单:</em></p>
<p><strong>Dockerfile</strong></p>
<pre><code>FROM colstrom/python:legacy
COPY requirements.txt /requirements.txt
RUN ["/usr/bin/pip", "install", "-r", "/requirements.txt"]
CMD ["/usr/bin/pip", "freeze"]
</code></pre>
<p><strong>要求.txt</strong></p>
<pre><code>selenium
</code></pre>
<p><strong>用pip包构建镜像</strong>希望你知道放置Dockerfile,要求.txt文件在新目录中。在</p>
<pre><code>D:\dockers\py1>docker build -t pypiptest .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM colstrom/python:legacy
-> 640409fadf3d
Step 2 : COPY requirements.txt /requirements.txt
-> abbe03846376
Removing intermediate container c883642f06fb
Step 3 : RUN /usr/bin/pip install -r /requirements.txt
-> Running in 1987b5d47171
Collecting selenium (from -r /requirements.txt (line 1))
Downloading selenium-3.0.1-py2.py3-none-any.whl (913kB)
Installing collected packages: selenium
Successfully installed selenium-3.0.1
-> f0bc90e6ac94
Removing intermediate container 1987b5d47171
Step 4 : CMD /usr/bin/pip freeze
-> Running in 6c3435177a37
-> dc1925a4f36d
Removing intermediate container 6c3435177a37
Successfully built dc1925a4f36d
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
</code></pre>
<p><strong>现在运行图像</strong>
如果您没有传递任何外部命令,那么container从<code>CMD</code>获取命令,它只显示<code>pip</code>包的列表。在这个例子中,<code>selenium</code>。在</p>
<pre><code>D:\dockers\py1>docker run -itd name testreq pypiptest
039972151eedbe388b50b2b4cd16af37b94e6d70febbcb5897ee58ef545b1435
D:\dockers\py1>docker logs testreq
selenium==3.0.1
</code></pre>
<p>因此,以上说明包安装成功。在</p>
<p>希望这对你有帮助。在</p>