Docker + K8s Workshop - Basics


Hello World

docker run hello-world

Explanation

Docker containers are run time instances of images. Think of a Docker image as a set of files (like the set of files we placed in the jail directory in the chroot demo).

In this case, we are asking docker to create a container with the image hello-world.

The image has to come from somewhere. By default, Docker searches in a public repository of images hosted by Docker at docker.io.

It fetches the image and then creates a container from the image (file system)

What is latest?

Docker convention uses latest to denote the very latest version of a particular image. This is just a convention - not all images have a version tagged latest.

By default, docker run tries to run the latest if no specific tag (version) is not specified.

The second time

docker run docker.io/hello-world:latest

This time Docker did not have to download a new image because this is the same image as before.

Image name

In docker run docker.io/hello-world:latest:

  • Registry: docker.io
  • Product/Service name hello-world
  • Version latest

Versioning

Versions can be denoted in any format:

Alpine:

docker run alpine:3.7 echo "Hello from Alpine"

OpenJDK 15:

docker run -it openjdk:15-slim-buster bash

Creating a custom image from scratch.

We will build an image from the files we used for the chroot example.

  • Download and unzip this [file]().
  • Build the image in the directory using docker build .
  • Run the container using docker run <id> /bin/bash -c /demo.sh

Creating a custom image from a Linux base.

FROM bash:latest
COPY demo.sh
docker build .
docker run <id> bash -c  /demo.sh

Controlling what gets run

CMD

CMD ["bash", "-c", "/demo.sh"]
docker run  <id>

ENTRYPOINT

Tagging an image

docker tag <id> demo-app:v1
docker image ls

Tag as latest

docker tag demo-app:v1 demo-app:latest

Tagging during build

docker build -t demo-app:v2 .

Accessing a running container

docker exec -it <container-id> sh

Note: It is the container id and not image id

This can be improved if we name our containers.

docker run demo-app:latest --name demo-app
docker exec -it demoa-app sh

Mounting volumes

Docker container’s disk is ephemeral. External storage can be attached using -v

docker run -v external-dir:/external demo-app:v1 bash -c /demo.sh