13

What is the point of using an OS in a docker container?

In the docker repository, you find an Ubuntu docker image: https://registry.hub.docker.com/_/ubuntu/ I thought Docker was more at the "app" level.

What is then the differences between an Ubuntu docker container and an Ubuntu Virtual Machine? If you have an entire OS in a docker container, isn't it pointless to use Docker then?

Thanks

1 Answer 1

14

You are lacking basic Docker concepts. It's a completely different thing.

The first thing you need to know is Docker's philosophy : run one process isolated in a container. You won't run an OS in a Docker container, you will run a process inside a container with a root filesystem content based on a linux distribution of your choosing. Ubuntu is a choice among others.

Now you should wonder how is it possible to get a process runing inside a linux base image different from the linux distribution your host is running with. For an OS to run you basically need :

  • A boot filesystem : contains the bootloader and the kernel that will reside in memory once loaded. We don't care about this in the case of Docker containers because the kernel is shared with the host and is the common part between all linux distributions.
  • A root filesystem : contains the filesystem structure. It may be different from one linux distribution to another. It's read-only until the boot sequence has finished.

Docker uses UnionFS to manage layers of disk blocks inside a container so you can pile them.

Behind the scenes, it uses an union mount which allows multiple filesystems to be mounted at the same time, appearing like a whole virtual one. It in fact drops the base image layer as read-write mode on top of the base root filesystem in read-only mode.

Here you have a pile of disk blocks layered in a way that the linux distribution the base image comes from would contain the same filesystem once installed in a real host, but it's inside a container this time.

The last thing lacking now is : how do you run this thing isolated ?

The answer is : namespaces. I won't go into the details here because it would deviate a bit from the original question. But what you need to know is that since kernel 2.4.19, namespaces of various kinds have appeared along the years. Currently the following namespaces are available :

  • IPC : IPC namespace (interprocess communications)
  • MNT : mount namespace
  • NET : network namespace
  • PID : pid namespace
  • USER : user namespace (uid)
  • UTS : UTS namespace (hostnames)

Namespaces are isolated structures inside the kernel that allow processes to run with a particular environment. For instance MNT namespace will be the key feature to get a process running in the base image root filesystem specificities. NET namespace will be another key feature for a container to have specific network interfaces in order to communicate with the docker bridge etc.

So, yes, the main purpose of all of this is to run an application isolated, ship it from your local environment to production easily with inside a box called container.

It would be a good idea to read docker's documentation before digging further into it.

7
  • Thanks, does that mean that a "Ubuntu" image like this one: registry.hub.docker.com/_/ubuntu makes "no sense" by itself and serve as a base for more defined container like a "nginx container"? I do not know if I make sense?
    – Michael
    Jan 21, 2015 at 21:00
  • Yes you do. That's exactly what's it's used for. And usually you will hear people call them base images for this reason. Jan 21, 2015 at 21:09
  • I have gone through the documentation and there is one thing I am not sure. Let's say the size of the ubuntu image is 140Mb. Then, another image based on this image adds 10 more Mb. If I run 2 containers from this image, will each container be 150Mb and I need 300Mb of disk space OR th total size will remain 150Mb? Thanks
    – Michael
    Jan 22, 2015 at 21:01
  • Wow, I just downloaded the official python image and the virtual size is 802.4 MB. It took a while to download. If I run 10 containers from it, how much space the containers will take? Thanks
    – Michael
    Jan 22, 2015 at 21:26
  • 2
    @YAmikep Some images can be big due to dependencies not properly cleaned. If you start 10 containers with the same image then it doesn't mean the disk space used will be 10 times the size of the image. You donwload an image once but run it from multiple containers. Docker use copy-on-write when running multiple containers from the same image which basically means that it will share the image between containers and create a private copy of a file inside an image only if the container add/modifies it. So the actual size used is linked to the activity inside containers, not how many runs. Jan 22, 2015 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .