DEV Community

Cover image for Getting Started with Docker and Kubernetes Sandboxes (Day 3)
Emmanuel Oghre
Emmanuel Oghre

Posted on

Getting Started with Docker and Kubernetes Sandboxes (Day 3)

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Build the Docker image:

docker build -t todoapp-docker .
Enter fullscreen mode Exit fullscreen mode

Verify the image is built:

docker images

Enter fullscreen mode Exit fullscreen mode

Pushing the Image to a Public Repository

Create a public repository on https://hub.docker.com/.

Login to Docker Hub:

docker login

Enter fullscreen mode Exit fullscreen mode

Tag your image with your username and repository details:

docker tag todoapp-docker:latest username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

Push the image:

docker push username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

Running the Container Locally

Pull the image from your repository (if running on another machine):

docker pull username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

Start the container and map port 3000:

docker run -dp 3000:3000 username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

Access your app at http://localhost:3000 (if successful).

Additional Commands

Entering Container:

docker exec -it containername sh

Enter fullscreen mode Exit fullscreen mode

(or use container ID)

Viewing Logs:

docker logs containername
Enter fullscreen mode Exit fullscreen mode

(or use container ID)

Inspecting Container:

docker inspect containername

Enter fullscreen mode Exit fullscreen mode

Cleaning Up Images:

docker image rm image-id
Enter fullscreen mode Exit fullscreen mode

_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!
_

Top comments (0)