Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determine OS distribution of a docker image

I need to determine the OS distribution name for any docker image. I can tag ubuntu:latest as image1:latest, but I should be able to get the distribution information of image1:latest when it is launched.

For achieving this, I used the below mentioned command to determine the OS version:

$ docker tag ubuntu image1 $ $ docker run -it image1 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py" ('Ubuntu', '14.04', 'trusty') $ 

However, this has a dependency on whether the image has python2 or python3 in it. It fails for ubuntu:12.04 and I need to use python2 there.

$ docker run -it ubuntu /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py" ('Ubuntu', '14.04', 'trusty') $ $ docker run -it ubuntu:12.04 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py" /bin/sh: 1: python3: not found $ $ docker run -it ubuntu:12.04 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python2 test.py" ('Ubuntu', '12.04', 'precise') $  

Q1. Is there a way I can achieve the same without knowing which version of python is there in a particular image?

NOTE: The goal is to determine which was the base image used to build this image. I don't have access to the Dockerfile used to build this image.

Q2. There is another approach of using entrypoint. I can build a separate image from the current image using Dockerfile. Or, I can specify entrypoint in cmdline when creating container but I need the script to be accessible within the container. I am guessing that I might need shared storage when using cmdline, is there a better way to achieve this? Any pointers would be really helpful.

Thanks.

like image 569
Rahul Avatar asked Feb 28 '16 23:02

Rahul


People also ask

How do I find the OS of a container?

The best method is to review the Dockerfile that was used the build the image. Linuxkit is the embedded Docker VM, which is the host OS with containers run on Docker for Windows. "uname" will give you details about the underlying host, not the container (even if you run it inside a container).

What is the OS of a Docker container?

The Docker platform runs natively on Linux (on x86-64, ARM and many other CPU architectures) and on Windows (x86-64). Docker Inc. builds products that let you build and run containers on Linux, Windows and macOS.

Does a Docker image contain the OS?

Every image contains an complete os. Special docker made OS's come with a few mega bytes: for example linux Alpine which is an OS with 8 megabytes! But bigger OS like ubuntu/windows can be a few gigabytes.

Do Docker containers share OS?

Yes, they do. Every container is based on an OS image, e.g. Alpine, CentOS or Ubuntu. They just share the host kernel, but run every user-space process in a separate name space specific for that container.


1 Answers

The Filesystem Hierarchy Standard has a standard definition for /etc/os-release, which should be available on most distributions:

The /etc/os-release and /usr/lib/os-release files contain operating system identification data.

The basic file format of os-release is a newline-separated list of environment-like shell-compatible variable assignments. It is possible to source the configuration from shell scripts.

This means you can just source /etc/os-release and use $NAME or $ID to identify the distribution. As an example, on Fedora it looks like this:

% source /etc/os-release % echo $NAME Fedora % echo $ID fedora 

On Debian:

% source /etc/os-release % echo $NAME Debian GNU/Linux % echo $ID debian 
like image 145
morxa Avatar answered Sep 21 '22 16:09

morxa