我创建了一个微服务(https://github.com/staticdev/enelvo-microservice),它需要克隆一个 git 存储库以创建一个 docker 映像,使用单阶段 Dockerfile 最终映像有 759MB:
FROM python:3.7.6-slim-stretch
# set the working directory to /app
WORKDIR /app
# copy the current directory contents into the container at /app
COPY . /app
RUN apt-get update && apt-get install -y git \
&& pip install -r requirements.txt \
&& git clone https://github.com/tfcbertaglia/enelvo.git enelvo-src \
&& cd enelvo-src \
&& python setup.py install \
&& cd .. \
&& mv enelvo-src/enelvo enelvo \
&& rm -fr enelvo-src
EXPOSE 50051
# run app.py when the container launches
CMD ["python", "app.py"]
我尝试了使用多阶段构建(https://blog.bitsrc.io/a-guide-to-docker-multi-stage-builds-206e8f31aeb8)的方法来减少没有 git 和 apt-get 列表的图像大小(来自更新):
FROM python:3.7.6-slim-stretch as cloner
RUN apt-get update && apt-get install -y git \
&& git clone https://github.com/tfcbertaglia/enelvo.git enelvo-src
FROM python:3.7.6-slim-stretch
COPY --from=cloner /enelvo-src /app/enelvo-src
# set the working directory to /app
WORKDIR /app
# copy the current directory contents into the container at /app
COPY . /app
RUN pip install -r requirements.txt \
&& cd enelvo-src \
&& python setup.py install \
&& cd .. \
&& mv enelvo-src/enelvo enelvo \
&& rm -fr enelvo-src
EXPOSE 50051
# run app.py when the container launches
CMD ["python", "app.py"]
问题是,在这样做之后,最终大小变得更大(815MB)。知道在这种情况下可能出了什么问题吗?