4
votes

A beginner's question; how does Docker handle underlying operating system variations when using the RUN command?

Let's take, for example, a very simple Official Docker Hub Dockerfile, for JRE 1.8. When it comes to installing the packages for java, the Dockerfile uses apt-get:

RUN apt-get update && apt-get install -y --no-install-recommends ...

To the untrained eye, this appears to be a platform-specific instruction that will only work on Debian-based operating systems (or at least ones with APT installed).

How exactly would this work on a CentOS installation, for example, where the package manager would be yum? Or god forbid, something like Solaris.


If this pattern of using RUN to fork arbitrary shell commands is prevalent in docker, how does one avoid inter-platform, or even inter-version, dependencies?

i.e. what if the Dockerfile writer has a newer version of (say) grep than I do, and they've used some new CLI flag that isn't available on earlier versions?

The only two outcomes from this can be: (1) RUN command exits with non-zero exit code (2) the Dockerfile changes the installed version of grep before running the command.

2
I'm using Dockerfile for creating an image with CentOS. The first line in Docker file is 'FROM docker.io/centos:latest'. It's download basic CentOS image and add my layers (my next RUN commands) above.ItayB

2 Answers

1
votes

The common point shared by all Dockerfiles is the FROM statement. It is the first line in the file and indicates the parent Docker image you're building on. A typical base image could be one with Ubuntu (i.e.: https://hub.docker.com/_/ubuntu/). The snippet you share in your question would fit well in an Ubuntu image (with apt-get) but not in a CentOS image.

In summary, you're installing docker in your CentOS system, but you're building a Docker image with Ubuntu in it.

0
votes

As I commented in your question, you can add FROM statement to specify which relaying OS you want. for example:

FROM docker.io/centos:latest
RUN yum update -y
RUN yum install -y java
...

now you have to build/create the image with:

docker build -t <image-name> .

The idea is that you'll use the OS you are familiar with (for example, CentOS) and build an image of it. Now, you can take this image and run it above Ubuntu/CentOS/RHEL/whatever... with

docker run -it <image-name> bash

(You just need to install docker in the desired OS.