Can a Docker image specify which operating system it can use? Say one Image with Windows and another with RHEL? In this case how Docker will maintain two different operating systems?
2 Answers
Docker is composed of layers. At the beginning of any Dockerfile you specify the OS by typing e.g. FROM python:3
My belief is that if you were to add another OS. The image would retain the environment from the first OS and install the env of the second OS over that. So essentially, your image would have both environments.
If you create a python image from the command above and name it docker build -t 'this_python' .
then make a new Dockerfile with the first line: FROM this_python
so the new image has python already, and you can install anything over this.
Best practice is to keep your docker image as small as possible. Install only what is required.
A quick example
FROM python:3
FROM ubuntu:latest
RUN apt-get update
The above Dockerfile gives you an image with Python and Ubuntu installed. But this is not how you should do it. Better is to use FROM ubuntu:latest
and then install python over it.

- 2,618
- 1
- 12
- 42
-
Thanks for your reply. So, the image will create OS on its own when "docker run"? – Shakti Kumar Oct 16 '19 at 09:11
-
1When you do `docker build -t 'some_name' .` it creates an image with that OS. When you do `docker run` it runs the image as a container. Docker is just an isolated environment. If your code needs Python and 8 specific packages. You package it and can run the docker on Windows, Mac or Linux. You use it as an isolated environment and not an "OS". – DUDANF Oct 16 '19 at 09:14
-
So, we can build the image on a specific OS, but the container will use the docker's (Host OS) OS? – Shakti Kumar Oct 16 '19 at 09:34
-
1Docker uses the hypervisor in your computer to run the container. It bypasses your own OS and uses the hardward (hypervisor) underneathe. Why dont you giive [this](https://medium.com/@SaadAAkash/docker-for-dummies-literally-ab3fc6362d5f) a read – DUDANF Oct 16 '19 at 09:36
-
1Thank you. This really helps. Let me go through it. – Shakti Kumar Oct 16 '19 at 09:38
Docker image is just docker image. It doesn't depend on the OS on which you run the docker engine. For example, when you run your docker image on Windows, actually it run on docker engine, which was hosted by a Virtual host of linux.

- 976
- 8
- 12
-
If my docker image has a java application to run which is OS specific, how to ensure that docker engine is inline with java application? If docker engine is hosted in Linux, I cannot run a service that demands a different OS? – Shakti Kumar Oct 16 '19 at 09:20
-
Actually, we can choose the base image, CentOS, Ubuntu and so forth. – todaynowork Jun 16 '20 at 08:06