我有一个运行我的应用程序的工作 docker 镜像,但我遇到的问题是,每当我添加新的依赖项时,我都必须重新安装所有依赖项。那太糟糕了。我知道我可以通过将依赖项放在单独的行上来解决这个问题,但这很笨拙,而且如果我想从不同的位置构建,那么它就不那么便携了。我更愿意做的是根据我现在知道的需求制作一个基础镜像(尤其是那些需要很长时间安装的镜像),然后从中构建所有新镜像,这样我就可以有快速的构建时间从任何机器。那么,综上所述,从 Dockerfile 创建基本镜像的好方法是什么,或者有没有更好的方法来实现我正在寻找的可移植性和快速构建时间?
最佳答案
任何 docker 镜像都是基础镜像。您可以使用 from tag
使用您从存储库构建或提取的任何图像作为基础图像。
正在进行的工作 ( Docker issue332 ) 能够展平基本图像以加快下载速度,但尚未完成。只要您没有在基本镜像中定义任何端口和卷,您就可以使用 Solomon 在关于此问题的评论中建议的 hack,即
Currently the only way to "squash" the image is to create a container from it, export that container into a raw tarball, and re-import that as an image. Unfortunately that will cause all image metadata to be lost, including its history but also ports, env, default command, maintainer info etc. --Solomon Hykes
为此,您可以运行:
# Run a NOOP command that creates a container
container_id=$(docker run -d <BASE-CONTAINER> ls)
# Run export the image as a tarball
docker export $container_id > image.tar
# Import the image into a new container
cat image.tar | docker import - yourname/BASE:TAG
# Now you can use ```from yourname/BASE:TAG``` in your docker files.
# Or you can push to dockerhub with the following
# commands so you can use on other machines
docker login
docker push yourname/BASE:TAG
关于docker - 从 Dockerfile 创建 Docker 基础镜像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516900/