Why Docker Has Ability to Run Different Linux Distribution

Why docker has ability to run different linux distribution?

Because the kernel is the same.

The common point of all linux distributions, and why they are called linux, is because they all use the linux kernel.

Containers share the same kernel as the host, that's why you can run an Arch image on a Ubuntu host.

Here's an overview of Linux.

The kernel is a part of the operating system that handles communication with the hardware. It's the lowest level of the operating system. Here is a list of the main functions of the kernel:

  • memory management
  • network management
  • device driver
  • file management
  • process management

So when you use a container you only have access to the kernel of the host, since it's the only part that communicates with hardware, as long as your OS uses the good syscall, you are able to run any linux distribution inside your container. (This is the reason you can't use Windows inside a container: it's not using the same syscall).

How am I able to use multiple operating system images within Docker?

Imagine your docker compose as a server farm. Each service (nginx, mariadb, ..) would be a physical server running an OS and its software. They are all connected via LAN within the same subnet. Each machine has its own IP address and there is a DNS and DHCP service running for giving the IPs and resolving names (service name = DNS-Alias). And there is a router blocking all connections from other subnets (= your host). Exceptions are configured by port mapping (=forwarding) ports: - 8000:8000.

So you can mix servers with all different OS variants of one type. This is due to the fact that docker is not a real virtual machine but more a VM light using the host OS resources to run the containers. So you can mix all kind of Linux distributions OR Windows versions. Every container uses the OS suiting it goals the best, e.g. Alpine for very small footprint and Ubuntu for much more comfort.

Docker: Mixing distros - any disadvantage?

Since the kernel is the same you can use any distro available: Why docker has ability to run different linux distribution?.

For example many projects are moving to Alpine Linux because it give you the ability to build very small images: see Docker Official Images are Moving to Alpine Linux.

If docker uses the underlying linux os, why specify the OS in the FROM line of a Dockerfile

I'm confused, though, as to why most Dockerfiles specify the OS in the
FROM line of the Dockerfile. I thought that as it was using the
underlying OS, then the OS wouldn't have to be defined.

I think your terminology may be a little confused.

Docker indeed uses the host kernel, because Docker is nothing but a way of isolating processes running on the host (that is, it's not any sort of virtualization, and it can't run a different operating system).

However, the filesystem visible inside the container has nothing to do with the host. A Docker container can run programs from any Linux distribution. So if I am on a Fedora 24 Host, I can build a container that uses an Ubuntu 14.04 userspace by starting my Dockerfile with:

FROM ubuntu:14.04

Processes running in this container are still running on the host kernel, but there entire userspace comes from the Ubuntu distribution. This isn't another "operating system" -- it's still the same Linux kernel -- but it is a completely separate filesystem.

The fact that my host is running a different kernel version than maybe you would find in an actual Ubuntu 14.04 host is almost irrelevant. There are going to be a few utilities that expect a particular kernel version, but most applications just don't care as long as the kernel is "recent enough".

So no, there is no virtualization in Docker. Just various (processes, filesystem, networking, etc) sorts of isolation.

What is the relationship between the docker host OS and the container base image OS?

As mentioned by BraveNewCurrency, the only relationship between the host OS and the container is the Kernel.

It is one of the main difference between docker and 'regular' virtual machines, there is no overhead, everything takes place directly within the host's kernel.

This is why you can run only Linux based distribution/binaries within the container. If you want to run something else, it is not impossible, but you would need some kind of virtualization within the container (qemu, kvm, etc.)

Docker manage images that are the file system representation. You can install any linux distribution or simply put binaries.

Indeed, for the convenience of the example, we often rely on the base images, but you could also create your image without any of the distribution libraries/binaries. That way you would have a really tiny yet functional container.

One more point regarding the distributions: as the kernel is still the kernel of the host, you will not have any specific kernel module/patches provided by the distribution.

Is it necessary different Docker container for different os

The Dockerfile you have just provided will create a Docker image that can run on all Operating systems that shares the Linux kernel such as: Debian, Ubuntu, Centos, Fedora just to name a few. And this is one of the Docker purpose, to be able to run the same image on any host that run the Linux kernel.

However, as you specify CentOs (FROM centos) inside your Dockerfile the application that would be running inside the Docker container will be using CentOS as for its Operating System.

Run Different Linux OS in Docker Container?

I think this previous post may help you understand it a little more - Docker container isolation, does it care about underlying Linux OS?.

The crux of the matter is that if the Host OS is RedHat then it is the RedHat kernel which will be used by whatever build of Linux you run in your Docker container ie. Ubuntu in your example.

This comes down to understanding what the difference is between a Linux OS and a Linux Image. You will not be running a full Ubuntu OS inside the Docker Container but an image of Ubuntu.

For the purpose of your question think:-

OS = kernel + filesystem/libraries

Image = filesystem/libraries

The Ubuntu image running inside your Docker container is just the Ubuntu filesystem/libraries - it will not contain the Ubuntu kernel. This partly explains the efficiencies you get from a Docker container which is leveraging the Kernel (among other things) of the underlying Host.



Related Topics



Leave a reply



Submit