简体   繁体   English

从Dockerfile创建Docker基础映像?

[英]Creating Docker Base Image from a Dockerfile?

I have a working docker image running my app, but I have the issue that whenever I add new dependencies, I have to reinstall all my dependencies. 我有一个工作的docker镜像运行我的应用程序,但我有一个问题,每当我添加新的依赖项,我必须重新安装我的所有依赖项。 That sucks. 太糟糕了。 I know I can work around this by putting dependencies on separate lines, but that's clunky, and not as portable if I want to build from different locations. 我知道我可以通过将依赖项放在单独的行上来解决这个问题,但这很笨拙,如果我想从不同的位置构建,那就不是可移植的。 What I'd much rather do is make a base image with the requirements I know I need now (especially the ones that take a long time to install), and then just build all new images off of that so I can have fast build times from any machine. 我宁愿做的是制作一个基本图像,其中包含我现在需要的要求(尤其是那些需要很长时间才能安装的要求),然后只需构建所有新图像,这样我就可以快速构建从任何机器。 So, all of that said, what's a good way to create a base image from a Dockerfile, or is there a better way to accomplish the portability and quick build times I'm looking for? 所有这些都说,从Dockerfile创建基本映像的好方法是什么,还是有更好的方法来实现我正在寻找的可移植性和快速构建时间?

Any docker image is a base image. 任何泊坞窗图像都是基本图像。 You can use from tag to use any image you have built or pulled from a repository as your base image. 您可以使用from tag将您在存储库中构建或提取的任何图像用作基本图像。

There is work in progress ( Docker issue332 ) to be able to flatten base images for faster download but it is not yet complete. 正在进行的工作( Docker issue332 )能够压缩基础映像以便更快地下载但尚未完成。 As long as you are not defining any ports and volumes in the base image you can use the hack that Solomon suggested in the comments on this issue, ie 只要您没有在基本映像中定义任何端口和卷,就可以使用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. 目前,“压缩”图像的唯一方法是从中创建容器,将该容器导出到原始tarball中,然后将其重新导入为图像。 Unfortunately that will cause all image metadata to be lost, including its history but also ports, env, default command, maintainer info etc. --Solomon Hykes 不幸的是,这将导致所有图像元数据丢失,包括其历史记录以及端口,环境,默认命令,维护者信息等。 - Solomon Hykes

To achieve this you can run: 为此,您可以运行:

# 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

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

相关问题 为Ruby On Rails环境创建Docker镜像时出错(来自Dockerfile) - Error when creating a Docker image for Ruby On Rails environment (from a Dockerfile) 从DockerFile创建Docker映像时是否删除了临时文件? - Are temporary files deleted while creating a docker image from a DockerFile? 创建一个Docker基本映像 - Creating a docker Base Image 在Dockerfile中使用哪个Docker基本映像? - Which docker base image to use in the Dockerfile? 从 dockerfile 构建 docker 镜像 - building docker image from dockerfile Z469A31FD9D773110F14057BAECCDDD25Z。 使用 dockerfile 创建映像时出错 - DOCKER. Error when creating an image with dockerfile 为什么 docker 必须从 dockerfile 创建映像,然后从映像创建容器,而不是从 Dockerfile 创建容器? - Why does docker have to create an image from a dockerfile then create a container from the image instead of creating a container from a Dockerfile? 从docker镜像获取dockerfile / docker命令 - Get dockerfile / docker commands from docker image 尝试使用 Dockerfile 和 podman 从 Docker Hub 中提取基础映像时出现身份验证错误 - Authentification error when trying to pull base image from Docker Hub using Dockerfile and podman 带有入口点的Dockerfile仅来自基本映像 - Dockerfile with entrypoint only from base image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM