从Dockerfile创建Docker基础映像?

我有一个工作的docker镜像运行我的应用程序,但我有一个问题,每当我添加新的依赖项,我必须重新安装我的所有依赖项.太糟糕了.我知道我可以通过将依赖项放在单独的行上来解决这个问题,但这很笨拙,如果我想从不同的位置构建,那就不是可移植的.我宁愿做的是制作一个基本图像,其中包含我现在需要的要求(尤其是那些需要很长时间才能安装的要求),然后只需构建所有新图像,这样我就可以快速构建从任何机器.所有这些都说,从Dockerfile创建基本映像的好方法是什么,还是有更好的方法来实现我正在寻找的可移植性和快速构建时间? 最佳答案 任何泊坞窗图像都是基本图像.您可以使用from标记将您在存储库中构建或提取的任何图像用作基本图像.

正在进行的工作(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
点赞