Cloud Native 12 min read

Dockerfile Best Practices: Alpine Images, Multi‑Stage Builds, ARG, CMD/ENTRYPOINT, and COPY vs ADD

This article explains Docker fundamentals and advanced Dockerfile techniques—including using Alpine base images, multi‑stage builds, build‑time ARG variables, CMD versus ENTRYPOINT usage, and the differences between COPY and ADD—to create smaller, faster, and more flexible container images.

IT Services Circle
IT Services Circle
IT Services Circle
Dockerfile Best Practices: Alpine Images, Multi‑Stage Builds, ARG, CMD/ENTRYPOINT, and COPY vs ADD

Docker is a container technology that creates isolated environments on a host OS, allowing independent installation of software and services.

Port mapping ( -p ) and volume mounting ( -v ) connect host ports and directories to containers.

Example command:

docker run -p 3000:3000 -v /aaa:/bbb/ccc --name xxx-container xxx-image

Docker images are built from Dockerfiles using docker build and run with docker run . A typical workflow includes building the image, pushing to a registry, and pulling for deployment.

To reduce image size, replace the default Debian‑based image with a lightweight Alpine base, e.g., node:18-alpine3.14 , which can shrink the image by hundreds of megabytes.

Multi‑stage builds separate the build environment from the runtime image, copying only the compiled dist folder and production dependencies, dramatically decreasing final image size.

Build‑time arguments ( ARG ) allow passing variables during docker build , which can be set as runtime environment variables ( ENV ) inside the container.

Using CMD provides a default command that can be overridden at docker run time, while ENTRYPOINT defines an immutable command; combining both gives flexible defaults.

The COPY instruction copies files as‑is, whereas ADD also extracts tar archives automatically.

Use Alpine base images for smaller size.

Apply multi‑stage builds to keep only necessary artifacts.

Leverage ARG for build flexibility and ENV for runtime variables.

Prefer CMD for overridable defaults; combine with ENTRYPOINT when needed.

Use COPY for regular file copying and ADD only when archive extraction is required.

DockerAlpineMulti‑Stage BuildDockerfileCMDARGCOPY vs ADDENTRYPOINT
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.