我需要在不同的CPU架构上运行python,比如AMD64和ARM (Raspberry )等等。我从Docker那里读到了多阶段构建的文档。
FROM python:3.10-slim
LABEL MAINTAINER=XXX
ADD speedtest-influxdb.py /speedtest/speedtest-influxdb.py
ENV INFLUXDB_SERVER="http://10.88.88.10:49161"
RUN apt-get -y update
RUN python3 -m pip install 'influxdb-client[ciso]'
RUN python3 -m pip install speedtest-cli
CMD python3 /speedtest/speedtest-influxdb.py
FROM arm32v7/python:3.10-slim
LABEL MAINTAINER=XXX
ADD speedtest-influxdb.py /speedtest/speedtest-influxdb.py
ENV INFLUXDB_SERVER="http://10.88.88.10:49161"
RUN apt-get -y update
RUN python3 -m pip install 'influxdb-client[ciso]'
RUN python3 -m pip install speedtest-cli
CMD python3 /speedtest/speedtest-influxdb.py但正如你所看到的,有一点需要重复。有更好的办法吗?
发布于 2022-07-16 16:57:48
多阶段构建的一个典型用法是构建要运行或部署的工件,然后将其COPY到不包含构建工具的最终映像中。当您想要构建几个独立的东西,或者在同一件事情上构建几个变体时,它通常不是正确的工具。
在您的示例中,除了FROM行之外,所有图像都是相同的。您可以使用ARG来指定正在构建的映像,FROM (在这种特殊情况下 ARG优于FROM)
ARG base_image=python:3.10-slim
FROM ${base_image}
...
CMD ["/speedtest/speedtest.py"]
# but only once如果您只是docker build这个映像,您将得到默认的base_image,它将对当前的默认架构使用默认的base_image。但是您可以请求一个不同的基本图像,或者一个不同的目标平台。
# Use the ARM32-specific image
docker build --build-arg base_image=arm32v7/python:3.10-slim .
# Force an x86 image (with emulation if supported)
docker build --platform linux/amd64 .https://stackoverflow.com/questions/73005600
复制相似问题