简体   繁体   English

多阶段 dockerfile:如何在没有 --user pip3 参数的情况下安装?

[英]Multistage dockerfile: how to install without --user pip3 argument?

I'm new to docker, and I'm not entirely sure why this works.我是 docker 的新手,我不完全确定为什么会这样。

I tried writing my dockerfile without the --user argument supplied to pip3 to install my requirements.txt file, but for some reason, copying the setup outputs from the ${INSTALL} dir to the ${APP} dir isn't working for me.我尝试在没有提供给pip3--user参数的情况下编写我的 dockerfile 来安装我的requirements.txt文件,但由于某种原因,将设置输出从${INSTALL}目录复制到${APP}目录不起作用我。

How can I write this such that I don't need to rely on the --user argument supplied to pip3 ?我怎样才能写这个,这样我就不需要依赖提供给pip3--user参数?

Using said argument works for now, but it's just really bugging me that I can't make this do what I want it to do.使用上述论点目前有效,但让我感到烦恼的是我无法让它做我想要它做的事情。

I build the image using this command:我使用以下命令构建图像:

docker image build --no-cache -t apptest .

Dockerfile文件

FROM python:3.8-alpine as base
ENV APP="/app"
ENV INSTALL="/install"
ENV LOC="/root/.local"
ENV LOCBIN="${LOC}/bin"
ENV PATH $PATH:${LOC}:${LOCBIN}

FROM base as builder
WORKDIR ${INSTALL}

COPY ./requirements.txt requirements.txt

#install all dependencies
RUN apk add --no-cache curl python3 pkgconfig python3-dev openssl-dev libffi-dev musl-dev make gcc
RUN pip3 install --user --no-cache-dir -r requirements.txt

FROM base

COPY --from=builder ${LOC} ${LOC}
COPY --from=builder ${LOCBIN} ${LOCBIN}
COPY ./src/ ${APP}
COPY ./src/Cogs/ ${APP}/Cogs
COPY ./src/Documentation/ ${APP}/Documentation
COPY ./src/Models/ ${APP}/Models
COPY ./src/OneNight/ ${APP}/OneNight

WORKDIR ${APP}

#start the service
ENTRYPOINT [ "python3", "service.py" ]
  1. When you install Python packages, code goes all over the place.当你安装 Python 包时,代码到处都是。 Eg libraries go into site-packages, scripts go into bin/, etc.. So you need to install everything in one place to make it easy to copy.例如,库进入站点包,脚本进入 bin/ 等。所以你需要把所有东西都安装在一个地方,以便于复制。 Your options are --user or a virtualenv, basically, or copying lots of different directories.您的选项基本上是--user或 virtualenv,或者复制许多不同的目录。 See https://pythonspeed.com/articles/multi-stage-docker-python/https://pythonspeed.com/articles/multi-stage-docker-python/

  2. That being said, you might not need a multi-stage build.话虽如此,您可能不需要多阶段构建。 You want one here because you're using a compiler.你想要一个在这里是因为你使用的是编译器。 But you might only need a compiler because you're using Alpine Linux.但是您可能只需要一个编译器,因为您使用的是 Alpine Linux。 For normal Linux distributions, most Python packages have binary precompiled wheels on PyPI, so much of the time you don't need a compiler at all.对于普通的 Linux 发行版,大多数 Python 包在 PyPI 上都有二进制预编译轮,所以很多时候你根本不需要编译器。

It's quite possible switching to Debian-based base image ( python:3.8-slim-buster ) might allow you to just install everything without compiler.很有可能切换到基于 Debian 的基本映像 ( python:3.8-slim-buster ) 可能允许您在没有编译器的情况下安装所有内容。 See https://pythonspeed.com/articles/alpine-docker-python/https://pythonspeed.com/articles/alpine-docker-python/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM