Loading...

10 Essential Docker Best Practices for Optimizing Container Performance

10 Essential Docker Best Practices for Optimizing Container Performance

Docker changed the way applications are deployed and run. For that to function at full throttle, one has to take care of the optimization of their containers. Here are ten best practices that help improve your Docker container performance, organized at the Build, Ship, and Run phases:

Build Phase

Use Official and Verified Base Images

Starting with official and verified base images means you are off to a great base of security and performance for your container.

Example:

FROM python:3.9-slim

Use Specific Image Versions

Always use a specific version of a base image to avoid any unexpected changes, in order to keep the builds consistent.

Example:

FROM node:14.17.0-alpine

Use Small Sized Official Images

Smaller base images, like Alpine, reduce the attack surface and improve build times.

Example:

FROM node:14-alpine

Use Multi-stage Builds

Multi-stage builds are a way to create smaller, more efficient Docker images. Isolation of the build environment from the runtime environment is possible, and the size of the final image dramatically goes down. This is where you’ll make sure to include in the final image only the necessary artifacts, reducing its size and potential security vulnerabilities.

Example:

FROM node:14 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:14-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package*.json ./
RUN npm install --production
CMD ["npm", "start"]

Minimize Layer Count

Every instruction in a Dockerfile adds a new layer. Fewer layers generally mean faster builds and smaller images. Combine related commands using the && operator and clean up in the same RUN instruction.

Example:

RUN apt-get update && \
      apt-get install -y python3 python3-pip && \
      apt-get clean && \
      rm -rf /var/lib/apt/lists/*

Leverage Docker Cache

Docker uses caching of intermediate layers between runs to speed up subsequent builds. Order your Dockerfile instructions from least to most frequently changing. This maximizes cache usage and reduces build times.

Example:

COPY package.json package-lock.json ./
RUN npm install
COPY . .

Use .dockerignore

A .dockerignore file will prevent unwanted files from being copied into the build context, reducing both build time and image size.

Example .dockerignore:

node_modules
npm-debug.log
Dockerfile
.git
.gitignore

Ship Phase

Scan Your Images for Security Vulnerabilities

Regularly scan Docker images against known vulnerabilities with Docker Scout, Trivy, or Snyk.

Example:

docker run -it \
  -e DOCKER_SCOUT_HUB_USER=<your Docker Hub user name> \
  -e DOCKER_SCOUT_HUB_PASSWORD=<your Docker Hub PAT>  \
  docker/scout-cli <command>

Run Phase

Use the Least Privileged User

Run all your applications with a nonprivileged user to maximize security.

Example:

FROM node:14-alpine
WORKDIR /app
COPY . .
RUN adduser -D myuser
USER myuser
CMD ["npm", "start"]

Implement Resource Limits

Set a memory and CPU limit for your containers so that contention for common resources is avoided and performance is consistent across different environments.

Example docker-compose.yml:

version: '3'
services:
app:
   image: myapp:latest
   deploy:
      resources:
      limits:
         cpus: '0.50'
         memory: 512M

The best practices provided here will enable you to create leaner, fitter Docker containers for better performance. Optimization in itself is a continuous process; therefore, based on changing requirements of your Dockerfiles and configurations of the containers, keep reviewing them to keep the performance at its peak.

As a developer, these practices have been invaluable to me for both my own personal and professional projects, helping me design lighter, faster, more reliable, and often more complex applications. I encourage you to experiment with these different techniques and see how they can help improve your Docker workflow.

Published on:

Learn more
Need help with this product?

We can help you with 10 Essential Docker Best Practices for Optimizing Container Performance

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

Home | Joseph Velliah
Home | Joseph Velliah

Fulfilling God’s purpose for my life

Share post:

Related posts

Prompt Injection, Hands-On: Attacking My Own Model With Bedrock Guardrails

I had read enough prompt-injection explainers. I still wanted to poke at a real filter and keep the bill and the teardown small. So I stood u...

1 month ago

Jev Does Not Paint: Putting TypeSafe in Front of Gemini

I wanted a reason to use TypeSafe that was not another chatbot wrapper. Most of the AI I wire into software still wants to talk. I needed some...

2 months ago

I Asked God to Hold My Hand

Almost 20 years ago, I was waiting outside my company to collect my documents and start my first IT job. I sat under a banyan tree. That day ...

2 months ago

Identity-Aware SRE Agents with kagent on Akamai LKE

I wanted a reason to put an AI agent in front of a real Kubernetes cluster and watch what happens when two different people ask it to fix the ...

2 months ago

Vasanam Studio: How I Built a Bible Verse Video Generator for My Church as a Hobby Project

Every morning at 5 AM, the women of my church gather for prayer. At the end of the session, our pastor’s wife shares a Bible verse and sends a...

5 months ago

The demo worked. That was the problem.

Over a weekend I built a small Kubernetes demo to play with zero trust. Three little services calling each other in a chain, a login page in f...

5 months ago

Notes from building an agent on AgentCore end to end

I wanted a reason to use AgentCore end to end. Runtime, memory, guardrails, identity, the whole thing. A Bible Q&A agent felt like a good ...

7 months ago

Building a Rust gRPC AI Security Gateway for LLM Traffic

I wanted a small, honest implementation of the GenAI governance shape in code: a component on every LLM call that applies policy first, option...

7 months ago

Claude Code Security: The Smart Way to Integrate AI

Anthropic just dropped Claude Code Security, and if you’re anywhere near AppSec or DevSecOps, you’ve probably already seen the debate lighting...

9 months ago

How I Built a Semantic Cache Using Only AWS Services

LLM calls are expensive and slow, but here’s the thing - users ask the same questions in different ways all the time. “What’s your refund poli...

9 months ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.