gpt4 book ai didi

docker - 具有多个基础的Docker容器?

转载 作者:行者123 更新时间:2023-12-02 18:10:22 25 4
gpt4 key购买 nike

我不太了解Docker的基本知识,特别是如果我想从同一个Dockerfile中的多个基础构建的话。例如,我知道这两行不起作用:

FROM X
FROM Y

(好吧,它将编译,但随后似乎仅将来自X的图像包含在最终版本中)。也许我错了,这是正确的,但我仍然没有看到任何其他像这样的Dockerfile。

我为什么要这样做?例如,如果X和Y是我想在DockerHub上找到的镜像,则我希望以此为基础。举一个具体的例子,如果我想要ubuntu并且我也想要python:
FROM python:2 
FROM ubuntu:latest

最好的方法是什么?我只限于一个基地吗?如果我想同时使用两者的功能,则应该进入docker文件,直到找到两者的共同点,然后通过在子镜像中一直手动复制dockerfile的代码之一来自己构建镜像,直到到达通用基础并将这些行添加到其他Dockerfile中?我认为这不是正确的方法,因为它似乎相当复杂,并且与Docker旨在提供的简单性不符。

最佳答案

For a concrete example if I wanted ubuntu and I also wanted python:

FROM python:2  
FROM ubuntu:latest


Ubuntu是Os,而不是python。因此,您需要安装了python的Ubuntu基本镜像。

您可以检查 offical python docker hub是基于 ubuntu的,因此在一张图片上您会得到 ubuntu + python ,那么为什么还要打扰两个FROM?这也不行。

Some of these tags may have names like buster or stretch in them. These are the suite code names for releases of Debian and indicate which release the image is based on. If your image needs to install any additional packages beyond what comes with the image, you'll likely want to specify one of these explicitly to minimize breakage when there are new releases of Debian.



所以对于你下面的问题

What is the best way to go about it? Am I just limited to one base? If I want the functionality from both am I supposed to go into the docker files



是的,将其限制为一个基本图像
python:3.7-stretch

因此,使用此基本镜像,您将同时拥有python和ubuntu。您不需要使Dockerfile具有两个 FROM

另外,您确实需要从头开始维护和构建镜像,使用官方镜像并根据需要进行扩展。

例如
FROM python:3.7-stretch
RUN apt-get update && apt-get install -y vim
RUN pip install mathutils

关于docker - 具有多个基础的Docker容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58402351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com