Docker Best Practices: Using ARG and ENV in Your Dockerfiles
ARG defines variables available only during the image build, while ENV defines variables available at build time and at container runtime. Worked examples show ARG values overridden with --build-arg or Compose build args. ENV cannot be declared without a value, so runtime values come from docker run -e, --env-file, or Compose environment interpolation via the shell, an inline variable, or a .env file. Rule of thumb: use ARG for build-only values and ENV for anything needed at runtime.
Related posts
- Accelerate Your Docker Builds Using AWS CodeBuild and Docker Build Cloud
Docker mediumAdeptOffloading container image builds from AWS CodeBuild to Docker Build Cloud addresses three common CI bottlenecks: undersized build machines, no persistent layer cache in clean CI environments, and slow emulated multi-architecture builds. A buildspec.yml installs the latest Buildx, logs in to ECR and Docker with a PAT from Secrets Manager, selects a cloud builder, and runs `docker buildx build --platform linux/amd64,linux/arm64 --push` straight to ECR. On a 2 vCPU/3GB CodeBuild instance, the build dropped from 5:59 for an AMD64-only local build to 1:04 for a native AMD64+Arm64 build, using 16 vCPU/32GB builders and a 200 GiB shared cache.
- Docker Best Practices: Understanding the Differences Between ADD and COPY Instructions in Dockerfiles
Docker mediumAdeptDockerfile COPY only copies files and directories from the build context into an image layer. ADD can do the same, but it also downloads URLs and automatically extracts local and remote archives. That extra behaviour brings flexibility, but also unpredictability and security risk from external content, so COPY is the sensible default and ADD should be reserved for cases that need fetching or extraction. Both instructions also work with remote build contexts such as Git repositories, remote tarball URLs and OCI images, with COPY still scoped to the context.
- Docker Best Practices: Choosing Between RUN, CMD, and ENTRYPOINT
Docker mediumAdeptRUN executes commands at image build time and creates layers, CMD sets a default command that any docker run arguments replace, and ENTRYPOINT fixes the executable so that run arguments are appended to it. Pairing an exec-form ENTRYPOINT with CMD as default arguments gives a container that acts like a standalone executable whose parameters users can override. Exec form is preferred for CMD and ENTRYPOINT because the process runs directly as PID 1 and receives signals such as SIGTERM, which allows graceful shutdown. Shell form runs under /bin/sh -c, which usually does not forward signals, so it fits only cases that need pipes, globbing or variable expansion. An ApacheBench example shows a URL argument overriding CMD and failing as a command.
- Introducing Docker Build Checks: Optimize Dockerfiles with Best Practices
Docker mediumAdeptDocker Desktop 4.33 adds Build checks, which evaluate Dockerfiles against codified best-practice rules during `docker build` and report warnings. Because the checks are part of the build itself, they also consider build arguments and base images rather than only linting the text. `docker build --check` runs the evaluation in under a second without executing the build, and `# check=error=true` makes warnings fail CI. Individual rules can be disabled with `# check=skip=...`, and the checks also work with multi-stage builds, Bake and Docker Build Cloud.
- Powered by Docker: Streamlining Engineering Operations as a Platform Engineer
Docker mediumAdeptReplacing QEMU-emulated multi-architecture Docker Buildx builds with native arm64 and amd64 self-hosted GitHub Actions runners cut build times by roughly 90%, with the multi-arch manifest published after the native builds finish. The runners live on separate Kubernetes node pools provisioned with Terraform and targeted via nodeSelector. Two further changes covered operations: SemVer-tagged images allow quick ECS rollbacks, and OpenTelemetry collector sidecars plus multi-stage Node.js Dockerfiles standardize telemetry collection and auto-instrumentation. The client reports about a 50% drop in lead time and time to restore, and about a 30% reduction in MTTD and MTTR.