2

I want to copy all of my python ,.py, files in my folder to my /app folder and according to this SO question I should be able to just do

FROM python:3.10.2-slim-bullseye

#Copy stuff into /app
COPY ./*.py /app

# set workdir as "/app"
WORKDIR /app 

#run
python train.py

but it throws the error mkdir /var/lib/docker/overlay2/jonf4h3njxr8zj28bxlyw7ztd/merged/app: not a directory when it reaches the third line WORKDIR /app.

I have tried several "versions" i.e COPY *.py /app, COPY /*.py /app but neither works

If I just copy everything i.e COPY . /app it works fine, but insted of floating my .dockerignore with stuff I don't need, I just want to copy my python-files only.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • Did you try setting the workdir before you make the copy. Also from the docker page of python, the work dir is located at /usr/src/app. ```WORKDIR /usr/src/app COPY *.py ./``` – mhaendler Sep 23 '22 at 06:12

2 Answers2

3

You need to create the app directory first

FROM python:3.10.2-slim-bullseye

# Create directory
RUN mkdir /app

#Copy stuff into /app
COPY ./*.py /app

# set workdir as "/app"
WORKDIR /app 

#run
python train.py

Harsh Kanjariya
  • 503
  • 5
  • 11
  • But why does it work when I just copy everything i.e `COPY . /app` ? – CutePoison Sep 23 '22 at 06:19
  • Because then it copies the folder, not files in the folder. – fredrik Sep 23 '22 at 06:34
  • But if the issue is that "app" doens't exist, then it shouldn't make any difference if I try to copy files or folders to "/app"? – CutePoison Sep 23 '22 at 06:36
  • 1
    when you use "COPY ." it copies a folder and renames it to "app" but when you use "COPY ./*.py" it copies files so it cannot find folder named "app" – Harsh Kanjariya Sep 23 '22 at 08:08
  • This shouldn't be necessary; [`COPY`](https://docs.docker.com/engine/reference/builder/#copy) is documented to create the destination directory if it doesn't exist (as does `WORKDIR`). – David Maze Sep 23 '22 at 10:15
2

The Dockerfile COPY directive has some fussy rules over the syntax of the right-hand side. The documentation currently states:

If multiple <src> resources are specified, either directly or due to the use of a wildcard, then <dest> must be a directory, and it must end with a slash /.

If <dest> does not end with a trailing slash, it will be considered a regular file and the contents of <src> will be written at <dest>.

So when you COPY a single *.py file to /app without a trailing slash, the Dockerfile creates a file named /app that's the single Python file. Then you get the error from WORKDIR that /app isn't a directory, it's the Python file.

Make sure the right-hand side of COPY ends with a slash:

COPY *.py /app/ # <-- ends with /
David Maze
  • 130,717
  • 29
  • 175
  • 215