From Docker Containers to Kubernetes

Kubernetes helps schedule containers across a clusters of nodes. Docker is widely used to provide a container runtime as well as build and image management. Docker defines a standard format for packaging and porting software, much like ISO containers define a standard for shipping freight. As a runtime instance of a Docker image, a container consists of three parts:

  • A Docker image

  • An environment in which the image is executed

  • A set of instructions for running the image

After an application has been packaged to run in a container, it is a containerized application. A containerized application image along with a set of declarative instructions can be passed to Kubernetes to deploy the application. The containerized app instance running on the Kubernetes node derives the container runtime from the Kubernetes node along with compute, network, and possibly storage resources.

Let's walk-through what it takes to move a Docker container to a Kubernetes cluster.

  1. Create a container image from a Dockerfile

  2. Build a corresponding YAML file to define how Kubernetes deploys the app

Dockerfile to Create a Hello World Container Image

A manifest, called a Dockerfile, describes how the image and its parts are to run in a container on a host. To make the relationship between the Dockerfile and the image concrete, here’s an example of a Dockerfile that creates a "Hello World" app from scratch:

FROM scratch
COPY hello /
CMD ["/hello"]

When you give this Dockerfile to a local instance of Docker by using the docker buildcommand, it creates a container template with the "Hello World" app installed.

Creating a Kubernetes Deployment for Hello World

We then define a deployment manifest to tell Kubernetes how to run "Hello World" based on the container template:

#Hello World Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld
spec:
  selector:
    matchLabels:
      app: hellowworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: helloworld
        image: boskey/helloworld
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"

Last updated