My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
How do you build your own Docker Image with zero dependencies?

How do you build your own Docker Image with zero dependencies?

Jan Vladimir Mostert's photo
Jan Vladimir Mostert
·Mar 20, 2016

I was wondering how one would proceed in building a docker image that doesn't depend on someone else's stuff that depends on someone else's stuff again that depends on someone else's stuff again etc etc.

Let's have a look at a few examples and try and trace it back ...

NodeJS: https://hub.docker.com/r/readytalk/nodejs/

This is their Dockerfile:

FROM debian:wheezy

RUN apt-get update -y && apt-get install --no-install-recommends -y -q curl python build-essential git ca-certificates
RUN mkdir /nodejs && curl http://nodejs.org/dist/v0.10.36/node-v0.10.36-linux-x64.tar.gz | tar xvzf - -C /nodejs --strip-components=1

ENV PATH $PATH:/nodejs/bin

Let's go to the official Debian Repo: https://hub.docker.com/_/debian/

FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/bash"]

Let's try the Java Repo: https://hub.docker.com/_/java/

#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM alpine:3.3

# A few problems with compiling Java from source:
#  1. Oracle.  Licensing prevents us from redistributing the official JDK.
#  2. Compiling OpenJDK also requires the JDK to be installed, and it gets
#       really hairy.

# Default to UTF-8 file.encoding
ENV LANG C.UTF-8

# add a simple script that can auto-detect the appropriate JAVA_HOME value
# based on whether the JDK or only the JRE is installed
RUN { \
echo '#!/bin/sh'; \
echo 'set -e'; \
echo; \
echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
} > /usr/local/bin/docker-java-home \
&& chmod +x /usr/local/bin/docker-java-home
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk/jre
ENV PATH $PATH:$JAVA_HOME/bin

ENV JAVA_VERSION 8u72
ENV JAVA_ALPINE_VERSION 8.72.15-r2

RUN set -x \
&& apk add --no-cache \
openjdk8-jre="$JAVA_ALPINE_VERSION" \
&& [ "$JAVA_HOME" = "$(docker-java-home)" ]

Let's check out alpine: https://hub.docker.com/_/alpine/

FROM scratch
ADD rootfs.tar.gz /

So it seems you can build your own Docker image from scratch simply by using the FROM scratch entry.

Looking at the scratch repo, it is confirmed: https://hub.docker.com/_/scratch/

FROM scratch This image is most useful in the context of building base images (such as debian and busybox) or super minimal images (that contain only a single binary and whatever it requires, such as hello-world).

As of Docker 1.5.0 (specifically, docker/docker#8827), FROM scratch is a no-op in the Dockerfile, and will not create an extra layer in your image (so a previously 2-layer image will be a 1-layer image instead).

So in short, scratch creates a blank docker container, then you can copy the stuff in that you need and proceed from there to build your own image from scratch.