Day 2/40 Days of K8s: Dockerize a Project ๐Ÿณ

Let's get into practicals by building a Docker image from a Dockerfile!

ยท

2 min read

Prerequisites:

  1. Docker Installation:

Steps:

  1. Clone the repo:

     git clone https://github.com/docker/getting-started-app.git
    
  2. Navigate to the project directory:

     cd getting-started-app/
    
  3. Create a Dockerfile:

     vi Dockerfile
    
  4. Add the following content to the Dockerfile:

# Use an official base image of Linux OS where our app will run
FROM node:18-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy all files from the current directory to /app in the container
COPY . .

# Install dependencies and build your application
RUN yarn install --production

# Command to run on container startup
CMD ["node", "src/index.js"]

# Expose the port your app listens on
EXPOSE 3000
  1. Build Docker Image:

     docker build -t vivekmanne/todo-app:v1 .
    
  2. List Docker Images:

     docker images
    
  3. Run Docker Container:

     docker run -dp 3000:3000 vivekmanne/todo-app:v1
    
  4. View Running Containers:

     docker ps
    
  5. Exec into Container Shell:

     docker exec -it d437e2fbaec1 /bin/sh
    
  6. View Container Logs:

    docker logs -f d437e2fbaec1
    
  7. Push Docker Image to Docker Hub:

    docker login
    docker push vivekmanne/todo-app:v1
    

Challenge:

For this simple application, the image is over 200MB:

REPOSITORY                TAG       IMAGE ID       CREATED             SIZE
vivekmanne/todo-app       v1        3dbe0cf498bb   About an hour ago   217MB

Optimization:

We'll optimize this to create a leaner image for:

  1. Fast processing / performance

  2. Less disk space consumption

  3. Reduced attack surface area

Our hero: "Multi-stage-Dockerfile" - Coming up in Day 3 of the CKA series!

Stay tuned for more Docker and Kubernetes insights! ๐Ÿš€ #40DaysOfKubernetes #Docker #Containerization #DevOps

ย