Docker and images

June 21, 2020 - Reading time: 5 minutes

Docker started as a python script on 2008, by 2012 it grew to a hundred independent microservices and it became a open source project on 2013.

In its infancy, Docker was just a wrapper around LXC, with additional functions. Some of those funcionalities:

  • Protable deployment. Allows us to build an application and its dependencies into a single object
  • Automatic build. Include tools to build appications from code.
  • Versioning.
  • Component re-use. Images can be built on top of existing images
  • Sharing. Via the public hub
  • Ecosystem. Docker defines an API that can be used for automation and orchestration.

The core is dockerd, it includes a REST API that could be invoked directly or with the docker cli. The "free" version is called docker-ce (enterprise is docker-ee)

Docker images

Images are divided into different layers. Docker uses the copy-on-write concept (COW) which means that we only create a copy of an object when we want to modify it.

[root@ip-10-0-1-100 cloud_user]# docker image pull alpine:latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

# a24bb4013296 is the image in our system 
[root@ip-10-0-1-100 cloud_user]# docker history alpine
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
a24bb4013296        3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:c92c248239f8c7b9b…   5.57MB

[root@ip-10-0-1-100 cloud_user]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              a24bb4013296        3 weeks ago         5.57MB

The Dockerfile used to created the alpine image is available here: https://hub.docker.com/_/alpine and hosted on github:

FROM scratch
ADD alpine-minirootfs-3.12.0-x86_64.tar.gz /
CMD ["/bin/sh"]

This is a very simple image, that builds on top of the docker reserved minimal image "scratch" https://hub.docker.com/_/scratch Scratch is used to build base images (like debian or busybox) or a super minimal image that just copies a beinary and runs it (like hello world)

More complex, multi-layer images, like httpd (the apache docker container) builds on top of a debian image, where it runs a series of commands to install dependencies and packages:

FROM debian:buster-slim
ENV HTTPD_PREFIX /usr/local/apache2
ENV PATH $HTTPD_PREFIX/bin:$PATH
RUN mkdir -p "$HTTPD_PREFIX" && chown www-data:www-data "$HTTPD_PREFIX"
WORKDIR $HTTPD_PREFIX

[...]

COPY httpd-foreground /usr/local/bin/
EXPOSE 80
CMD ["httpd-foreground"]

We can see all the layers when we pull the image:

[root@ip-10-0-1-100 cloud_user]# docker image pull httpd
Using default tag: latest
latest: Pulling from library/httpd
8559a31e96f4: Pull complete
bd517d441028: Pull complete
f67007e59c3c: Pull complete
83c578481926: Pull complete
f3cbcb88690d: Pull complete
Digest: sha256:387f896f9b6867c7fa543f7d1a686b0ebe777ed13f6f11efc8b94bec743a1e51
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

[root@ip-10-0-1-100 cloud_user]# docker image  history  httpd
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ccbcea8a6757        12 days ago         /bin/sh -c #(nop)  CMD ["httpd-foreground"]     0B
<missing>           12 days ago         /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           12 days ago         /bin/sh -c #(nop) COPY file:c432ff61c4993ecd…   138B
<missing>           12 days ago         /bin/sh -c #(nop)  STOPSIGNAL SIGWINCH          0B
<missing>           12 days ago         /bin/sh -c set -eux;   savedAptMark="$(apt-m…   60.9MB
<missing>           12 days ago         /bin/sh -c #(nop)  ENV HTTPD_PATCHES=           0B
<missing>           12 days ago         /bin/sh -c #(nop)  ENV HTTPD_SHA256=a497652a…   0B
<missing>           12 days ago         /bin/sh -c #(nop)  ENV HTTPD_VERSION=2.4.43     0B
<missing>           12 days ago         /bin/sh -c set -eux;  apt-get update;  apt-g…   35.4MB
<missing>           12 days ago         /bin/sh -c #(nop) WORKDIR /usr/local/apache2    0B
<missing>           12 days ago         /bin/sh -c mkdir -p "$HTTPD_PREFIX"  && chow…   0B
<missing>           12 days ago         /bin/sh -c #(nop)  ENV PATH=/usr/local/apach…   0B
<missing>           12 days ago         /bin/sh -c #(nop)  ENV HTTPD_PREFIX=/usr/loc…   0B
<missing>           12 days ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           12 days ago         /bin/sh -c #(nop) ADD file:4d35f6c8bbbe6801c…   69.2MB