1

So I am trying to dockerize a Discord bot I built. In the picture are my file structure and Dockerfile. The problem I have is that it doesn't copy requirements.txt.

Actually, I was watching a video on how to do it, but I ran into this problem. This is the first Dockerfile I have made.

What I aim to do: copying the whole file structure as is and when the image starts, it runs bot.py.

enter image description here

This is the Dockerfile written out.

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"]

The error that I keep getting is:

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


Tim Pierce
  • 25
  • 1
  • 6

1 Answers1

1

According to the error you get, the crux of the issue is this line:

COPY /requirements.txt /requirements.txt

which should be replaced with:

COPY requirements.txt requirements.txt

or equivalently with:

COPY requirements.txt ./

→ the first filename is a relative path w.r.t. the Docker build context
→ the second path is relative to the target working directory (WORKDIR)

Extra remarks

As an aside, note that the header of your Dockerfile is awkward:

FROM ubuntu:18.04
FROM python:3.9.4

should be replaced with:

FROM python:3.9.4

The rule of thumb is: each Dockerfile should contain only one FROM command (unless you want to use the so-called multi-stage build feature).

Finally, there are a few optimizations that can be seen as mandatory for your Dockerfile, e.g., the lines:

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

should be merged in:

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

and the two lines:

COPY . .

RUN pip3 install -r requirements.txt

should be swapped to:

RUN pip3 install -r requirements.txt

COPY . .

For more details on the underlying issues, you might be interested in these two other answers of mine:

Update after the OP's edit:

Finally it seems that the remaining issue comes from the fact your docker-build command is not correct:

Can you try replacing

docker build -t sinless777/helix_bot - < Dockerfile

with

docker build -t sinless777/helix_bot .

?

ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • I have made your suggested edits however I receive the same error. Thus I have updated the post with the current docker file and error output. – Tim Pierce Nov 17 '21 at 04:34
  • OK! FYI, it seems the very last issue comes from your `docker build …` command → as you had written `docker build -t … - < Dockerfile`, the context did not contain the `requirements.txt` file; hence the error… (see the last edit of my answer) – ErikMD Nov 17 '21 at 09:35