{"id":2170,"date":"2023-05-23T05:33:43","date_gmt":"2023-05-23T05:33:43","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=2170"},"modified":"2025-11-23T05:35:29","modified_gmt":"2025-11-23T05:35:29","slug":"how-to-create-a-docker-image-for-kubernetes-to-deploy","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/how-to-create-a-docker-image-for-kubernetes-to-deploy\/","title":{"rendered":"How to Create a Docker Image for Kubernetes to Deploy"},"content":{"rendered":"<hr \/>\n<h2>1. What Is a Docker Image?<\/h2>\n<p>A Docker image is a read-only, portable template that contains everything required to run your application:<\/p>\n<ul>\n<li>Application code<\/li>\n<li>Dependencies and libraries<\/li>\n<li>Operating system base layer<\/li>\n<li>Runtime (Node, Python, Go, etc.)<\/li>\n<li>Startup command<\/li>\n<\/ul>\n<p>Kubernetes uses these images to create containers inside Pods. Therefore, creating a clean, reliable Docker image is the foundation of a successful deployment.<\/p>\n<hr \/>\n<h2>2. Creating a Dockerfile<\/h2>\n<p>The first step is writing a <code>Dockerfile<\/code>. This defines how your application is built. Below is a simple example for a Node.js web application:<\/p>\n<pre><code class=\"language-dockerfile\"># Dockerfile\n\nFROM node:18-alpine\n\nWORKDIR \/app\n\nCOPY package*.json .\/\nRUN npm install --omit=dev\n\nCOPY . .\n\nEXPOSE 3000\n\nCMD [\"npm\", \"start\"]\n<\/code><\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<li><code>FROM node:18-alpine<\/code> chooses a lightweight base image.<\/li>\n<li><code>WORKDIR \/app<\/code> sets the working directory.<\/li>\n<li><code>COPY<\/code> and <code>npm install<\/code> install dependencies.<\/li>\n<li><code>EXPOSE 3000<\/code> informs Kubernetes which port the app listens on.<\/li>\n<li><code>CMD<\/code> runs the application.<\/li>\n<\/ul>\n<hr \/>\n<h2>3. Building a Docker Image<\/h2>\n<p>Once your Dockerfile is ready, build the image:<\/p>\n<pre><code>docker build -t my-app:1.0.0 .\n<\/code><\/pre>\n<p>This creates an image called <code>my-app<\/code> with the version <code>1.0.0<\/code>. It is good practice to use semantic versioning or Git commit hashes as tags.<\/p>\n<hr \/>\n<h2>4. Tagging the Image for Your Registry<\/h2>\n<p>To deploy to Kubernetes, your image must be stored in a registry such as:<\/p>\n<ul>\n<li>Docker Hub<\/li>\n<li>GitHub Container Registry<\/li>\n<li>Amazon ECR<\/li>\n<li>Google Artifact Registry<\/li>\n<li>Azure Container Registry<\/li>\n<\/ul>\n<p>Tag the image with your registry path:<\/p>\n<pre><code>docker tag my-app:1.0.0 myregistry\/my-app:1.0.0\n<\/code><\/pre>\n<hr \/>\n<h2>5. Pushing the Image to the Registry<\/h2>\n<p>Push the image so Kubernetes can pull it:<\/p>\n<pre><code>docker push myregistry\/my-app:1.0.0\n<\/code><\/pre>\n<p>At this point, your container image is globally accessible to your Kubernetes cluster.<\/p>\n<hr \/>\n<h2>6. Creating a Kubernetes Deployment<\/h2>\n<p>Once the image is in the registry, create a Kubernetes Deployment manifest to run the image. Below is a production-ready example:<\/p>\n<pre><code class=\"language-yaml\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: my-app\n  namespace: production\n  labels:\n    app: my-app\nspec:\n  replicas: 3\n  revisionHistoryLimit: 5\n  selector:\n    matchLabels:\n      app: my-app\n  template:\n    metadata:\n      labels:\n        app: my-app\n    spec:\n      securityContext:\n        runAsNonRoot: true\n        runAsUser: 1000\n        fsGroup: 1000\n      containers:\n        - name: my-app\n          image: myregistry\/my-app:1.0.0\n          ports:\n            - containerPort: 3000\n          resources:\n            requests:\n              cpu: \"100m\"\n              memory: \"128Mi\"\n            limits:\n              cpu: \"300m\"\n              memory: \"256Mi\"\n          readinessProbe:\n            httpGet:\n              path: \/\n              port: 3000\n            initialDelaySeconds: 5\n            periodSeconds: 10\n          livenessProbe:\n            httpGet:\n              path: \/\n              port: 3000\n            initialDelaySeconds: 10\n            periodSeconds: 20\n          securityContext:\n            readOnlyRootFilesystem: true\n            allowPrivilegeEscalation: false\n<\/code><\/pre>\n<hr \/>\n<h2>7. Applying the Deployment<\/h2>\n<p>To deploy your application:<\/p>\n<pre><code>kubectl apply -f deployment.yaml\n<\/code><\/pre>\n<p>Kubernetes will:<\/p>\n<ul>\n<li>Pull the image from the registry<\/li>\n<li>Create Pods<\/li>\n<li>Perform a rolling update if an older version exists<\/li>\n<li>Attach readiness and liveness probes<\/li>\n<li>Maintain the desired replica count<\/li>\n<\/ul>\n<hr \/>\n<h2>8. Verifying the Deployment<\/h2>\n<pre><code>kubectl get deploy my-app -n production\nkubectl get pods -n production -o wide\nkubectl describe deploy my-app -n production\n<\/code><\/pre>\n<p>These commands will confirm that Kubernetes successfully rolled out your new Docker image.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What Is a Docker Image? A Docker image is a read-only, portable template that contains everything required to run your application: Application code Dependencies and libraries Operating system base layer Runtime (Node, Python, Go, etc.) Startup command Kubernetes uses these images to create containers inside Pods. Therefore, creating a clean, reliable Docker image is the foundation of a successful<a href=\"https:\/\/nicktailor.com\/tech-blog\/how-to-create-a-docker-image-for-kubernetes-to-deploy\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[148],"tags":[],"class_list":["post-2170","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=2170"}],"version-history":[{"count":3,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":2173,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2170\/revisions\/2173"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}