Skip to main content

Command Palette

Search for a command to run...

ReadList #3 - Optimizing Docker Builds:

A part of the 60-day ReadList series – Simplifying Docker & Kubernetes, one post at a time!

Updated
2 min read
ReadList #3 - Optimizing Docker Builds:
V

Hey there! I'm Vishwa, a DevOps Engineer. Curious about me? Check out my profile and posts, but here's a quick overview. I've spent the past two years building credibility, consistently working on projects, and upskilling myself. I'm a self-learner, and my posts reflect my journey. When it comes to technical expertise, I'm proficient in Linux, Networking, Shell Scripting, Docker, Ansible, Terraform, Kubernetes, Prometheus, Grafana, Loki, Jenkins, GitHub Actions, Python, Go, and Java. Wondering about my cloud experience? I'm well-versed in AWS, Digital Ocean, and GCP. I'm also skilled in troubleshooting. My ability to self-motivate and search for solutions allows me to quickly adapt to new technologies. And yes, I love writing. Sharing knowledge through blogs is something I’m passionate about. To reach out to Me, You can do DM and also leave an E-mail here at vishwa20042003@gmail.com, Waiting for your Connections and Opportunities.

1. Multi-Stage Builds

A technique to reduce image size and build time by using multiple steps in a Dockerfile.

Why Multi-Stage Builds?

✅ Smaller final image (only required files included).
✅ Faster builds using cached layers.
✅ No unnecessary dependencies in the final container.

Example: Building a Go Application

# Stage 1: Build the application
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp

# Stage 2: Create a minimal runtime image
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]

How it works?

  • First golang:1.20 is used to compile the Go app.

  • The final image only contains the compiled binary (from builder).

  • No Go compiler is included in the final image → smaller & faster!


2. Useful Docker Commands

Running Commands in a Running Container (exec)

docker exec -it <container_id> sh  # For Alpine/Linux
docker exec -it <container_id> bash  # For Ubuntu/Debian
  • -i → Interactive

  • -t → Terminal access

  • Example: Running a shell inside an Nginx container

      docker exec -it my_nginx_container sh
    

3. Checking Logs of a Container (logs)

docker logs <container_id>
  • Example: Viewing logs of a running app

      docker logs myapp
    
  • Follow logs in real-time:

      docker logs -f <container_id>
    

4. Inspecting a Container (inspect)

docker inspect <container_id>
  • Gives detailed JSON output about the container (IP, volumes, environment variables).

  • Example: Checking container IP

      docker inspect mycontainer | grep "IPAddress"
    

5. Conclusion:

Optimizing Docker builds with multi-stage builds significantly reduces image size and build time. By splitting the build and runtime environments, you keep your final image lean and efficient. Additionally, mastering essential Docker commands like exec, logs, and inspect helps in debugging and monitoring containers effectively.

Keep container images lightweight, build efficiently, and ensure smooth container management by leveraging these best practices. Stay tuned for more insights on Docker and K8S!