如何为 Docker 创建自己的基本图像?

根据 Docker 文件,要构建您自己的映像,您必须始终使用 FROM指令指定一个基本映像。

显然,在 Docker 索引中有很多图像可供选择,但是如果我想建立自己的图像呢?这可能吗?

如果我理解正确的话,图像 base是在 Ubuntu 上构建的,我想尝试一个 Debian 图像。另外,我想真正了解 Docker 是如何工作的,而且 base映像对我来说仍然是一个黑盒子。


编辑: < a href = “ https://docs.docker.com/development/development-images/baseimages/”rel = “ noReferrer”> 关于创建基础映像的官方文档

86358 次浏览

You can take a look at how the base images are created and go from there.

You can find them here: https://github.com/dotcloud/docker/tree/master/contrib. There is mkimage-busybox.sh, mkimage-unittest.sh, mkimage-debian.sh

Quoting Solomon Hykes:

You can easily create a new container from any tarball with "docker import". For example:

debootstrap raring ./rootfs
tar -C ./rootfs -c . | docker import - flimm/mybase

(credit to fatherlinux) Get information from https://developers.redhat.com/blog/2014/05/15/practical-introduction-to-docker-containers/ , which explains better

  1. Create the tar files for your file system, simply could be

    tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos6-base.tar /
    
  2. Transfer the tar file to other docker system if not installed locally and import it

    cat centos6-base.tar | docker import - centos6-base
    
  3. Now you can verify by running it.

    docker run -i -t centos6-base cat /etc/redhat-release
    

The scripts from dotcloud combine first two steps together which make me confused and looks complicated in the beginning.

The docker official guideline using debootstrap also tries to make clean file system.

You can judge by yourself how to do step 1.

If you want to make your own base image I would first take a look at Official Images, specifically stackbrew inside that repo.

Otherwise there are some great references for minimal OS images in the docker repo itself.

For example here is a script for making a minimal arch image and there are more here.

To start building your own image from scratch, you can use the scratch image.

Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

FROM scratch
ADD hello /
CMD ["/hello"]

http://docs.docker.com/engine/articles/baseimages/#creating-a-simple-base-image-using-scratch