简体   繁体   中英

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. 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?

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.

There is work in progress ( Docker issue332 ) to be able to flatten base images for faster download but it is not yet complete. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM