Docker Images

Why this Matters

Traditional monolithic architectures are hard to scale. As an application's code base grows, it becomes complex to update and maintain. Introducing new features, languages, frameworks, and technologies becomes very hard, limiting innovation and new ideas. Within a microservices architecture, each application component runs as its own service and communicates with other services via a well-defined API. Microservices are built around business capabilities, and each service performs a single function. Microservices can be written using different frameworks and programming languages, and you can deploy them independently, as a single service, or as a group of services.

Introduction to the Build Process

In this introduction we'll cover an overview of Docker images, how docker uses layers to build images, and an introduction to the Dockerfile used by "docker build" command to create images. (Note case is important here, the "D" is upper case, and the rest in lower case).

The Dockerfile contains all the instructions needed to build an image, and it must be in the root of the project directory.

Building an image is done with the "docker build" command. Docker looks for a Dockerfile in the base of the directory. Use the -t option to name your image. The image is only available if the build completes successfully. Previously built layers will be reused. The -f flag can be used to specify a different Dockerfile.

Every container starts from an image. There are many images in the docker registry: to help developers get started.

Image Layers

Layers are Shared

Images are Shared

Base Images

What are the benefits of base images?

  • Don't Repeat Yourself
  • Containers should be ephemeral
  • Using a base system allows for inheritance
  • Base your images on official images to allow for reuse

Working with Files

Source paths are always relative to the Dockerfile project directory. You cannot use source files outside the Dockerfile project directory.
Commands: WORKDIR, COPY, ADD. It is recommended to always use absolute paths with WORKDIR.

FROM debian:stretch

FROM debian:stretch
COPY src dest can also use JSON format:
COPY ["src", "dest"]

FROM debian:stretch
COPY test.txt /data/test.txt

Building Example

The destination directory or parent directories don't have to exit before COPY command.
WORKDIR. Paths that do not end "/" are treated as a regular file. Paths that end in "/" is treated as a directory.

1> docker build -t dem0 .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM debian:stretch
stretch: Pulling from library/debian
219d2e45b4af: Pull complete
Digest: sha256:5fafd38cdee6c7e6b97356092b97389faa0aa069595f1c3cc3344428b5fd2339
Status: Downloaded newer image for debian:stretch
---> 72ef1cf971d1
Step 2/2 : COPY test.txt /data/test.txt
---> 7797f76796b5
Removing intermediate container 6da02b3061fa
Successfully built 7797f76796b5
Successfully tagged dem0:latest
0> docker run --rm -it dem0
root@a8de718353f0:/# cd /data
root@a8de718353f0:/data# ls -la
total 12
drwxr-xr-x 2 root root 4096 Oct 1 21:31 .
drwxr-xr-x 28 root root 4096 Oct 1 21:32 ..
-rw-r--r-- 1 root root 17 Oct 1 21:29 test.txt
root@a8de718353f0:/data# cat test.txt
I am a test file

ADD Command

The ADD command works like COPY with additional features. For example, it can fetch files from the internet and copy them to the destination. If neither the source url or the destination end with a slash, the file is downloaded and copied to the destination. If both the source url and the destination end with a slash, the downloaded file is copied into the destination directory with that name.
ADD https://root/something/ /dest/ .... it would be placed in /dest/something.
ADD can unpack local tar files. The files can be compressed with gzip, bzip2, and xz. This feature does not work for URLs, only local files.

Running Command



Docker cmdline examples:

0> docker container run -it --name=bb busybox:1.26
Unable to find image 'busybox:1.26' locally
1.26: Pulling from library/busybox
27144aa8f1b9: Pull complete
Digest: sha256:be3c11fdba7cfe299214e46edc642e09514dbb9bbefcd0d3836c05a1e0cd0642
Status: Downloaded newer image for busybox:1.26
/ # ps aux
1 root 0:00 sh
5 root 0:00 ps aux
/ # ls -la
Mem: 2111880K used, 1741328K free, 5892K shrd, 267036K buff, 1390336K cached
CPU: 0.0% usr 0.0% sys 0.0% nic 100% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 0.00 0.00 0.00 2/223 8
1 0 root S 1204 0.0 0 0.0 sh
8 1 root R 1196 0.0 0 0.0 top

/ # Crt-D (exit)
130> docker attach bb
You cannot attach to a stopped container, start it first
1> docker start bb
0> docker attach bb
/ #

Show Docker Disk Usage: system df

1> docker system df
Images 17 9 3.436GB 2.515GB (73%)
Containers 16 5 177.5MB 166MB (93%)
Local Volumes 5 3 74.92MB 106.7kB (0%)

Docker Reading List

Evolution of Containers - Docker, Solaris Zones, BSD Jails and more

The Docker Story

What is Docker by Docker

Who is using Docker and How

Docker vs Vagrant