Slim Docker Image contains git repository

Viewed 3290

I have a requirements.txt file which contains the following package:

git+https://username:password@gitlab.mycompany.com/mypackage.git@master#egg=mypackage

I am able to build my docker image using a basic dockerfile. However, I'm trying to use a more complex docker file to get my docker image to be as slim as possible:

FROM python:3.7-alpine as base
COPY . /app
WORKDIR /app

FROM base AS dependencies
COPY requirements.txt ./
RUN apk add --no-cache make automake gcc g++ git  && \
    pip install -r requirements.txt

FROM base
WORKDIR /app
COPY . /app
COPY --from=dependencies /root/.cache /root/.cache
COPY requirements.txt ./
RUN pip install -r requirements.txt && rm -rf /root/.cache
EXPOSE 8000
CMD python main.py

The problem is that during the last phase of the build I get error which 'git' cannot be found, i.e The build tries to pull 'mypackage' instead of taking it from the "dependencies" part. Any idea how to fix this?

The error:

 Error [Errno 2] No such file or directory: 'git': 'git' while executing command git clone -q     Cannot find command 'git' - do you have 'git' installed and in your PATH?
1 Answers

You don't have git in your last (3rd) image, because you only have git in dependencies, while the last one derives from base, which is pure alpine python.

So when you try to RUN pip install -r requirements.txt && rm -rf /root/.cache, you fail on requirement with git protocol.

If you need your final image to be slim, there are few options how to fix it:

  • use venv (Python's virtual environment); create it on 2nd step and COPY to last one. Then there no need to install requirements.

  • download reqs from repository to local disk on 2nd step, then COPY them to 3rd step and install (may need gcc on 3rd step, but not git)

Related