Getting Started with Docker and Kubernetes Sandboxes (Day 3)
Emmanuel Oghre
Posted on June 24, 2024
This post builds on the concepts covered in Day 2's video and assumes you already have Docker. If you haven't, you can use these resources:
Docker Sandbox: Link
Kubernetes Sandbox: Link
Download Docker Desktop: Link
Docker multi-stage builds have several advantages over traditional single-stage builds. They're pretty cool, actually!
First off, you can make your images smaller. How? Well, by splitting the build process into stages, you can leave out all the stuff that's not needed in the final image. You know, things like build dependencies and compilers that the application itself doesn't need to run. So, only the essential application artifacts end up in the slimmed-down image. That means smaller images that don't take up much storage space or time to download. Neat, right?
But wait, there's more! These multi-stage builds also beef up your security. Smaller images mean there's less room for attackers to find vulnerabilities. By getting rid of unnecessary components, you're reducing the potential weak spots that bad guys can exploit. Safety first!
Another perk is faster builds. Docker can cache intermediate images created during the build process. So, if the build instructions in a particular stage haven't changed, Docker can just reuse the cached image instead of rebuilding it from scratch. This can really speed things up, especially for complex applications. Time is money, my friend!
And let's not forget about enhanced maintainability. Breaking down the build process into stages makes it easier to read and tweak your Dockerfile. Each stage focuses on a specific task, so you can understand and modify the process without any headaches. It's all about simplicity and keeping things organized.
So, to sum it all up, Docker multi-stage builds are an awesome technique for creating lean, secure, and efficient Docker images.
Demo Setup
Clone the sample repository (or use your own web application):
git clone https://github.com/piyushsachdeva/todoapp-docker.git
cd todoapp-docker/
Create a Dockerfile and paste the content provided in the video.
FROM node:18-alpine AS installer
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:latest AS deployer
COPY --from=installer /app/build /usr/share/nginx/html
Build the Docker image:
docker build -t todoapp-docker .
Verify the image is built:
docker images
Pushing the Image to a Public Repository
Create a public repository on https://hub.docker.com/.
Login to Docker Hub:
docker login
Tag your image with your username and repository details:
docker tag todoapp-docker:latest username/new-reponame:tagname
Push the image:
docker push username/new-reponame:tagname
Running the Container Locally
Pull the image from your repository (if running on another machine):
docker pull username/new-reponame:tagname
Start the container and map port 3000:
docker run -dp 3000:3000 username/new-reponame:tagname
Access your app at http://localhost:3000 (if successful).
Additional Commands
Entering Container:
docker exec -it containername sh
(or use container ID)
Viewing Logs:
docker logs containername
(or use container ID)
Inspecting Container:
docker inspect containername
Cleaning Up Images:
docker image rm image-id
_Refer to the CKA2024 Series by Piyush Sahdeva
Github Repo: Link
This post provides a basic overview of the commands covered in the video. Refer to the video for detailed explanations and troubleshooting. Happy coding!
_
Posted on June 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.