Choosing the right Docker base image

Depending on our end goal, using the image of our favorite Linux distribution might or might not be the best solution. Starting with a full CentOS container image might be a waste of resources, while an Alpine Linux image might not contain the most complete libc for our usage. In other cases, using the image from our favorite programming language might also be a good idea, or not. Let's see this in depth and learn when to choose what source.

Getting ready

To step through this recipe, you will need a working Docker installation.

How to do it…

Most common distributions are available as a container form.

Starting from an Ubuntu image

Ubuntu ships official images that are all tagged with both their release version and name: ubuntu:16.04 is equivalent to ubuntu:xenial. At the time of writing, the supported Ubuntu releases are 12.04 (precise), 14.04 (trusty), 16.04 (xenial), and 16.10 (yakkety).

To start with an Ubuntu image in a Dockerfile, execute the following:

FROM ubuntu:16.04
ENTRYPOINT ["/bin/bash"]

Starting from a CentOS image

The CentOS team ships official container images, all tagged with versions. It's highly recommended that you stick with the rolling builds that are continuously updated because these are tagged only with major versions such as centos:7. At the time of writing, the supported CentOS releases are CentOS 7, 6, and 5. If for some compliance reason we were to use a specific CentOS 7 release, specific tags such as centos:7.3.1611, centos:7.2.1511, centos:7.1.1503, and centos:7.0.1406 are available.

To start with the latest CentOS 7 available, in a Dockerfile execute the following:

FROM centos:7
ENTRYPOINT ["/bin/bash"]

Starting from a Red Hat Enterprise Linux (RHEL) image

Red Hat also ships containers for RHEL. At the time of writing, images are hosted on Red Hat's Docker registry servers (https://access.redhat.com/containers/). These images aren't tagged with release versions, but directly with their name: rhel7 for RHEL 7 and rhel6 for RHEL 6. Similarly, subversions are also directly in the name of the image: RHEL 7.3 has the image named rhel7.3.

To start with the latest RHEL 7, in a Dockerfile execute the following:

FROM registry.access.redhat.com/rhel7
ENTRYPOINT ["/bin/bash"]

Starting from a Fedora image

Fedora is officially built for Docker and each release is simply tagged with its version number. Fedora 25 has fedora:25, and it goes back to fedora:20 at the time of writing.

To start a with the latest Fedora release, use the following in a Dockerfile:

FROM fedora:latest
ENTRYPOINT ["/bin/bash"]

Starting from an Alpine Linux image

Alpine Linux is a very popular and secure lightweight Linux distribution in the container world. It's dozens of times smaller in size than other main distributions: less than 5 MB. It became so popular that Docker (the company) is now using it as a base for all its official images—and the Alpine founder is now working at Docker. Alpine versions are found in the image tags: Alpine 3.1 is alpine:3.1, and similarly, Alpine 3.4 is alpine:3.4.

To start with the 3.4 release of Alpine Linux, use this in a Dockerfile:

FROM alpine:3.4
ENTRYPOINT ["/bin/sh"]

Starting from a Debian image

The Debian distribution is present as well, with many different tags: we can find the usual debian:stable, debian:unstable, and debian:sid we're used to, and also some other tags, such as debian:oldstable. Release names are tagged like the corresponding versions, so the image debian:8 is the same as debian:jessie. Debian ships slim images for each release: debian:jessie-slim is 30% smaller than the main one (80 MB compared to 126 MB at the time of writing).

To start with the Debian 8 (Jessie) release, use the following in a Dockerfile:

FROM debian:jessie
ENTRYPOINT ["/bin/bash"]

Linux distributions container image size table

Here's a table with the current size for each referenced image:

Linux distribution image

Size

Alpine 3.4

4.799 MB

Debian 8 (slim)

80 MB

Debian 8

123 MB

Ubuntu 16.04

126.6 MB

RHEL 7.3

192.5 MB

CentOS 7.3

191.8 MB

Fedora 25

199.9 MB

With this information in hand, we can now decide to go for any one of these.

That being said, many popular programming languages (Go, Node, Java, Python, Ruby, PHP, and more) are also shipping their own container images. They are all very often based on the images from the operating system container images in the preceding table. It will be interesting to use them if our product is definitely going to use the corresponding language as they often offer custom versions and features.

Starting from a Node JS image

The official repository for the Node Docker image includes many tagged versions with many base images: node:7 is based on Debian Jessie, while node:7-alpine is based on Alpine 3.4. node:7-slim will be based on the slim Debian Jessie, and there's even node:7-wheezy if we feel like running Node 7 on Debian Wheezy. Also available are Node 6, 4, and below.

To start from the latest Node 7 image version, use this in a Dockerfile:

FROM node:7
ENTRYPOINT ["/bin/bash"]

For the record, a node:7 image will be around 650 MB, while node:4-slim will be around 205 MB.

Starting from a Golang image

Go is well distributed as a Docker image. Its releases are tagged by release (such as golang:1.7) and with alternatives such as one based on Alpine (golang:1.7-alpine) or even for Windows Server (golang:1.7-windowsservercore and golang:1.7-nanoserver).

To start from the Go image, use the following in a Dockerfile:

FROM golang:1.7
ENTRYPOINT ["/bin/bash"]

The main Go 1.7 image is 672 MB.

Starting from a Ruby image

Ruby is also distributed as an official Docker image: all the latest releases are found tagged like ruby:2.3. Alternative builds from Alpine Linux and Debian Jessie slim images are also available.

Note

A distinct Ruby-on-Rails Docker image used to exist, but is now deprecated in favor of the main Ruby Docker image.

To start from the Ruby 2.3 image, use the following to start a Dockerfile:

FROM ruby:2.3
ENTRYPOINT ["/bin/bash"]

The main Ruby 2.3 image is 725 MB.

Starting from a Python image

Python is officially distributed and many of its versions are supported as tagged Docker images. We can find versions 2.7, 3.3, 3.4, 3.5, and current beta versions based on Debian Jessie or Wheezy, Alpine, and Windows Server.

To start our project using the Python 3.5 image, add the following in a Dockerfile:

FROM python:3.5
ENTRYPOINT ["/bin/bash"]

The main python:3.5 image is around 683 MB.

Starting from a Java image

Java users are also getting official releases on Docker. Both OpenJDK and JRE are available, for versions 6, 7, 8, and 9, based on Debian Jessie or Alpine.

To start using the OpenJDK 9 image, use the following in a Dockerfile:

FROM openjdk:9
ENTRYPOINT ["/bin/bash"]

The main openjdk:9 image is 548 MB—one of the smallest programming language images available.

Starting from a PHP image

The PHP Docker image is very popular, and available in many different flavors. It's one of the easiest ways of easily testing newer and older releases of PHP on a platform. PHP 5.6 and 7.0 (and all beta versions) are available, and each is also available with a different flavor, that is, based on Alpine (php:7-alpine), Debian Jessie with Apache (php:7-apache), or Debian Jessie with FPM (php:7-fpm), but if we still like FPM with Alpine, it's also ok (php:7-fpm-alpine).

To start using a classic PHP 7 Docker image, start with the following in a Dockerfile:

FROM php:7
ENTRYPOINT ["/bin/bash"]

The main php:7 image is 363 MB—this is the smallest programming language image available.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.81.206