What is the proper way to reference a Dockerfile when creating a Deployment in Kubernetes?

11/16/2020

Let's say I have a deployment that looks something like this:

apiVersion: v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 3
  template:
    kind: Pod
    metadata: myapp-pod
    labels:
      apptype: front-end
    containers:
    - name: nginx
      containers: <--what is supposed to go here?-->

How do I properly build a container using an existing Dockerfile without having to push a build image up to Docker hub?

-- Nick Saccente
docker
dockerfile
kubernetes

2 Answers

11/16/2020

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment check this out.

Make sure you give a good read at the documentation :)

-- Kroustou
Source: StackOverflow

11/16/2020

Kubernetes can't build images. You all but are required to use an image registry. This isn't necessarily Docker Hub: the various public-cloud providers (AWS, Google, Azure) all have their own registry offerings, there are some third-party ones out there, or you can run your own.

If you're using a cloud-hosted Kubernetes installation (EKS, GKE, ...) the "right" way to do this is to push your built image to the corresponding image registry (ECR, GCR, ...) before you run it.

<!-- language: lang-sh -->
docker build -t gcr.io/my/image:20201116 .
docker push gcr.io/my/image:20201116
<!-- language: lang-yaml -->
containers:
  - name: anything
    image: gcr.io/my/image:20201116

There are some limited exceptions to this in a very local development environment. For example, if you're using Minikube as a local Kubernetes installation, you can point docker commands at it, so that docker build builds an image inside the Kubernetes context.

<!-- language: lang-sh -->
eval $(minikube docker-env)
docker build -t my-image:20201116 .
<!-- language: lang-yaml -->
containers:
  - name: anything
    image: my-image:20201116 # matches `docker build -t` option
    imagePullPolicy: Never   # since you manually built it inside the minikube Docker
-- David Maze
Source: StackOverflow