102

According to the Docker documentation, to build your own image, you must always specify a base image using the FROM instruction.

Obviously, there are lots of images to choose from in the Docker index, but what if I wanted to build my own? Is that possible?

The image base is built off Ubuntu if I understand correctly, and I want to experiment with a Debian image. Plus, I want to really understand how Docker works, and the base image is still a blackbox for me.


Edit: official documentation on creating a base image

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Doesn't matter. :-) Questions here still have to be on-topic according to the site guidelines. They're covered on the [help] and [about] pages. (The link there also seems to pertain to developers of Docker containers.) If you're asking about your own container image, there's a tool for that [on their site](http://docs.docker.io/en/latest/use/builder/#dockerbuilder_) – Ken White Aug 16 '13 at 19:47
  • @KenWhite: relevant meta question: http://meta.stackexchange.com/questions/179249/why-is-this-docker-question-off-topic – Flimm Aug 16 '13 at 19:57
  • :-) Relevant [meta answer](http://meta.stackexchange.com/a/179250/172661) at this point in time, which is the answer right above yours. (See especially the second comment to the answer I linked, about the clarity of what your question is asking.) – Ken White Aug 16 '13 at 20:02
  • 9
    @Ken White: the question is how to 'make' a base image, not how to deploy an 'image'. Flimm is trying to program something that can be deployed afterwards, by him or by others. Amazon EC2 is deployment related, but I think they still have a couple of programmers programming programmy stuff :). Creating base images is an example of programmy stuff I would say. – qkrijger Aug 17 '13 at 22:08
  • @qkrijger: This is being discussed in the linked meta question. Since that discussion is open, please post any discussion of the topicality there (where the consensus seems to be it's not a "programmy stuff" topic). – Ken White Aug 17 '13 at 22:21
  • @Flimm to create a new base image you can make `FROM scratch`, using the scratch image which is provided for said purpose http://blog.xebia.com/2014/07/04/create-the-smallest-possible-docker-container/. You could alternatively start with a debian installation http://www.aossama.com/build-debian-docker-image-from-scratch/ – That Realty Programmer Guy Apr 28 '15 at 06:07

5 Answers5

35

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

creack
  • 116,210
  • 12
  • 97
  • 73
  • 1
    The mkimage-* scripts have been moved and renamed (as has the official repo) to [here](https://github.com/docker/docker/tree/master/contrib/mkimage) – tsalaroth Apr 09 '17 at 15:03
32

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
Community
  • 1
  • 1
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Perhaps post the edit/update in a comment instead so other can see it and perhaps someone can re-submit the edit. – Bdoserror Oct 24 '13 at 15:26
  • 1
    @Flimm Why would I need to use sudo in this case? Run these commands in your user directory and you should have no reason for privilege elevation. You are creating an image, not installing it. – rancidfishbreath Jan 15 '14 at 21:57
  • Nor do you even need root for normal Docker use; simply add yourself to the `docker` group. – tekknolagi May 20 '14 at 14:02
  • 1
    But the hyphen in the import command, recommended by Flimm is necessary: `docker import - flimm/mybase` instead of `docker import flimm/mybase` – Daniel Alder Aug 27 '14 at 14:17
  • Ownership of the files in the tar will be affected by whether you are running as root or not. If not as root, then `debootstrap` and `tar` should at least be run under `fakeroot`. – clacke Apr 30 '16 at 10:18
22

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

Larry Cai
  • 55,923
  • 34
  • 110
  • 156
14

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

Mike
  • 288
  • 3
  • 9
  • 1
    This plus `import` from a tar file, are the two canonical answers, and that link is the canonical resource. – clacke Apr 30 '16 at 10:20
6

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Jessie Frazelle
  • 635
  • 5
  • 9