如何让我的 docker 文件复制 requirements.txt 并正确公开端口

How to get my docker file to copy requirements.txt and expose the ports properly

所以我正在尝试对我构建的 Discord 机器人进行 docker 化。图中是我的文件结构和Dockerfile。我遇到的问题是它不复制 requirements.txt.

实际上,我正在观看有关如何操作的视频,但我 运行 遇到了这个问题。这是我制作的第一个Dockerfile。

我的目标是:按原样复制整个文件结构,当图像启动时,它会运行 bot.py

这是写出来的Dockerfile。

FROM python:3

WORKDIR /Helix

COPY requirements.txt ./

RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

EXPOSE 3306:3306
EXPOSE 443:443
EXPOSE 50:50

RUN apt-get update \
 && apt-get install libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y \
 && pip install pyaudio

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "bot.py"]

我不断收到的错误是:

I:\HELIX\Modules\Discord\Helix>docker build -t sinless777/helix_bot - < Dockerfile
failed to get console mode for stdin: The handle is invalid.
[+] Building 1.7s (7/11)
 => [internal] load build definition from Dockerfile                                                                                                  0.1s
 => => transferring dockerfile: 468B                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/python:3                                                                                           1.2s
 => [internal] load build context                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                       0.0s
 => CANCELED [1/7] FROM docker.io/library/python:3@sha256:437585501d11ef4b4b831cf8a6d6611eb526e327006d506bcedcfdea3fde442a                            0.3s
 => => resolve docker.io/library/python:3@sha256:437585501d11ef4b4b831cf8a6d6611eb526e327006d506bcedcfdea3fde442a                                     0.0s
 => => sha256:437585501d11ef4b4b831cf8a6d6611eb526e327006d506bcedcfdea3fde442a 2.60kB / 2.60kB                                                        0.0s
 => => sha256:3d11a72c123ffc9326b86ad432480cef0ab73f02b7ed684873c2f8c35ce9a79f 2.22kB / 2.22kB                                                        0.0s
 => => sha256:4246fb19839fd033a0dd925c1f89cd1ad482c6b703d56f34bf0d2808b076e132 8.61kB / 8.61kB                                                        0.0s
 => CACHED [2/7] WORKDIR /Helix                                                                                                                       0.0s
 => ERROR [3/7] COPY requirements.txt ./                                                                                                              0.0s
------
 > [3/7] COPY requirements.txt ./:
------
failed to compute cache key: "/requirements.txt" not found: not found


根据您得到的错误,问题的症结在于这一行:

COPY /requirements.txt /requirements.txt

应替换为:

COPY requirements.txt requirements.txt

或等效于:

COPY requirements.txt ./

→第一个文件名是相对路径w.r.t。 the Docker build context
→ 第二个路径是相对于目标工作目录的(WORKDIR)

补充说明

顺便说一句,请注意您的 Dockerfile 的 header 很尴尬:

FROM ubuntu:18.04
FROM python:3.9.4

应替换为:

FROM python:3.9.4

经验法则是:每个 Dockerfile 应该只包含一个 FROM 命令(除非你想使用 the so-called multi-stage build feature)。

最后,有一些优化对于您的 Dockerfile 来说是强制性的,例如:

RUN apt-get update
RUN apt-get install libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y
RUN pip install pyaudio

应该合并到:

RUN apt-get update \
 && apt-get install libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y \
 && pip install pyaudio

和两行:

COPY . .

RUN pip3 install -r requirements.txt

应该换成:

RUN pip3 install -r requirements.txt

COPY . .

有关潜在问题的更多详细信息,您可能对我的另外两个答案感兴趣:

OP's edit后更新:

最后,剩下的问题似乎来自于您的 docker-build 命令不正确:

你能试试更换吗

docker build -t sinless777/helix_bot - < Dockerfile

docker build -t sinless777/helix_bot .

?