Q:

COPY .tar.gz needed in Dockefile or not?

Docker newbie here, quick question about the COPY command while dockerizing Django.
I thought the COPY command is used to copy over a local file into the Docker container/context or else, as far as Docker is concerned, during the build the file does not exist.
The development Dockerfile from the tutorial I'm following had this section:
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

So I understood that without that COPY command during the build the second RUN command would fail because it has no idea what requirements.txt is. Therefore, since I wanted to install my django-polls app tarball, I added a COPY and tacked on the phrase to the second RUN:
# install dependencies
COPY ./requirements.txt .
COPY ./django-polls-0.1.tar.gz .
RUN pip install --upgrade pip && pip install -r requirements.txt && pip install django-polls-0.1.tar.gz

As you can see, I combined the two RUN commands, added the COPY .tar.gz line, and tacked the .tar.gz installation on to the end of the RUN command. So I have 2 questions, the second one being more of the focus of this post:

Was it necessary to move the pip install --upgrade pip into the final RUN command? I understand that two different RUN commands do not affect each other, therefore the original, first RUN was doing nothing.

Was it necessary to add the second COPY command? I ask because I thought it was, but then I realize I omitted it in my production Dockerfile and it was still able to install django-polls.

That is, my production Dockerfile has these lines:
# install dependencies
COPY ./requirements.txt .
RUN pip install --upgrade pip && pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt && pip install django-polls-0.1.tar.gz

And even without a COPY ./django-polls-0.1.tar.gz . line, it was able to install django-polls. What am I not understanding here?
Thanks

A:

Was it necessary to move the pip install --upgrade pip into the final RUN command? I understand that two different RUN commands do not affect each other, therefore the original, first RUN was doing nothing.

It was not really necessary to work, but it is somehow a best practice, as every RUN command you add in your Dockerfile, is adding a new Layer to the resulting Docker Image. So, if possible, and you do not benefit from Caching (yayers are cached during built) you should combine it into one RUN command.

Was it necessary to add the second COPY command? I ask because I thought it was, but then I realize I omitted it in my production Dockerfile and it was still able to install django-polls.

For the COPY command you totally understood how it works. So yes this should be necessary.

And even without a COPY ./django-polls-0.1.tar.gz . line, it was able to install django-polls. What am I not understanding here?

This can not work from what you listed here. Only reason why that works, may be because the file was somehow added/installed in a previous step, which i cannot tell, as you did not list the complete Dockerfile here.
Best regards Shogoki

