Day 2/40 Days of K8s: Dockerize a Project ๐ณ
Let's get into practicals by building a Docker image from a Dockerfile!
Table of contents
Prerequisites:
Docker Installation:
Use Play with Docker (dockerhub credentials required)
(Or) Install Docker Desktop locally (recommended for future use) https://www.docker.com/products/docker-desktop/
Steps:
Clone the repo:
git clone https://github.com/docker/getting-started-app.git
Navigate to the project directory:
cd getting-started-app/
Create a Dockerfile:
vi Dockerfile
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
Build Docker Image:
docker build -t vivekmanne/todo-app:v1 .
List Docker Images:
docker images
Run Docker Container:
docker run -dp 3000:3000 vivekmanne/todo-app:v1
View Running Containers:
docker ps
Exec into Container Shell:
docker exec -it d437e2fbaec1 /bin/sh
View Container Logs:
docker logs -f d437e2fbaec1
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:
Fast processing / performance
Less disk space consumption
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