How to build and run an app with Docker and Kubernetes

10 lug 2023 3 min di lettura
How to build and run an app with Docker and Kubernetes
Indice dei contenuti

Introduction

Welcome to this tutorial on Docker and Kubernetes, two essential tools for developing and deploying applications in a microservices architecture. These two tools have become industry standards for container management and cluster orchestration, respectively.

In this tutorial, we'll walk you through the steps of building and running a simple application using Docker and Kubernetes. You'll learn how to create a Docker image for your application, run the image in a Docker container, and finally deploy your container on a Kubernetes cluster.

This tutorial is intended for those who already have a basic understanding of Docker and Kubernetes. If you are new to these tools, we recommend that you familiarize yourself with the basic concepts before proceeding. If, on the other hand, you are already familiar with Docker and Kubernetes and want to deepen your knowledge, this tutorial is ideal for you.

Before you begin, make sure you have Docker and Kubernetes installed on your computer. Also, you'll need a text editor to write the code for your application.

Part 1: Docker

Docker allows us to containerize our applications, making it possible to run in any environment that supports Docker.

Step 1: Install Docker

Visit the official Docker site and follow the instructions to install Docker on your operating system.

Step 2: Create a Dockerfile

A Dockerfile is a script that contains instructions for building a Docker image. Let's create a simple Dockerfile for a Node.js application.

Create a new file called Dockerfile and insert the following code:

# Usa l'immagine ufficiale Node.js per la fase di build
 FROM node:14 AS build
 WORKDIR /app
 COPY package*.json./RUN npm install
 COPY..

 # Fase di esecuzione
 FROM node:14-alpine
 WORKDIR /app
 COPY --from=build /app.
 EXPOSE 8080
 CMD [ "node", "app.js" ]

This Dockerfile first create an image with Node.js and copy your source code into the image (build phase). Then create a smaller image based on Node.js Alpine and copy the application from the build stage. The application runs on port 8080.

Step 3: Building the Docker image

Run the following command to build the Docker image:

docker build -t my-node-app.

Step 4: Run the Docker image

Once the image is built, you can run it with the following command:

docker run -p 8080:8080 my-node-app

Your application should now be running and accessible on port 8080.

Part 2: Kubernetes

Kubernetes allows us to easily manage, scale and deploy our containerized applications.

Step 1: Install Kubernetes

Visit the official Kubernetes site and follow the instructions to install Kubernetes on your operating system.

Step 2: Create a Kubernetes configuration file

A Kubernetes configuration file (also called a manifest) defines the resources you want to create in your Kubernetes cluster. Let's create a simple configuration file for our Node.js application.

Create a new file called my-node-app.yaml and insert the following code:

apiVersion: v1
 kind: Service
 metadata:
 name: my-node-app
 labels:
 app: my-node-app
 spec:
 ports:
 - port: 8080
 targetPort: 8080
 selector:
 app: my-node-app
 ---
 apiVersion: apps/v1
 kind: Deployment
 metadata:
 name: my-node-app
 spec:
 replicas: 3
 selector:
 matchLabels:
 app: my-node-app
 template:
 metadata:
 labels:
 app: my-node-app
 spec:
 containers:
 - name: my-node-app
 image: my-node-app:latest
 ports:
 - containerPort: 8080

This file defines a service and deployment for our application. The deployment creates three pods, each with a copy of our application, and the service makes the application accessible on port 8080.

Step 3: Run the application in the Kubernetes cluster

Run the following command to create the resources defined in the configuration file:

kubectl apply -f my-node-app.yaml

Your application should now be running in your Kubernetes cluster and accessible on port 8080.

Conclusion

Congratulations! You've just created a containerized application with Docker and run that application in a Kubernetes cluster. Remember, this is just a basic tutorial. Docker and Kubernetes offer many other features that allow you to build powerful and scalable applications.

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.