---
title: "Docker Best Practices: Using ARG and ENV in Your Dockerfiles"
description: "ARG defines variables available only during the image build, while ENV defines variables available at build time and at container runtime."
url: "https://swe.quest/articles/docker-best-practices-using-arg-and-env-in-your-dockerfiles-97d31347b92062d6"
canonical: "https://www.docker.com/blog/docker-best-practices-using-arg-and-env-in-your-dockerfiles/"
source: "Docker"
published: "2024-10-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["containers","configuration"]
---

# Docker Best Practices: Using ARG and ENV in Your Dockerfiles

A post on [Docker](https://swe.quest/sources/docker.md), published 2024-10-16, 6 min read, How-to. Original: [https://www.docker.com/blog/docker-best-practices-using-arg-and-env-in-your-dockerfiles/](https://www.docker.com/blog/docker-best-practices-using-arg-and-env-in-your-dockerfiles/)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Containers](https://swe.quest/tags/containers.md) (Infrastructure)
- [Configuration management](https://swe.quest/tags/configuration.md) (Reliability & operations)

## Prerequisites

- Dockerfile basics
- Docker Compose basics ([easy posts on Containers](https://swe.quest/tags/containers.md?difficulty=easy))

## Keywords

Docker, Dockerfile, Docker Compose, ARG, ENV

## Related posts

- [Accelerate Your Docker Builds Using AWS CodeBuild and Docker Build Cloud](https://swe.quest/articles/accelerate-your-docker-builds-using-aws-codebuild-and-docker-build-cloud-cf566617782de575.md): Docker · 2024-12-18 · Medium · 8 min read · CI/CD, Containers, Build systems, Cloud platforms
  Offloading 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.
- [Docker Best Practices: Understanding the Differences Between ADD and COPY Instructions in Dockerfiles](https://swe.quest/articles/docker-best-practices-understanding-the-differences-between-add-and-copy-4a75b284bd6c2baa.md): Docker · 2024-08-08 · Medium · 6 min read · Containers, Supply chain security
  Dockerfile COPY only copies files and directories from the build context into an image layer.
- [Docker Best Practices: Choosing Between RUN, CMD, and ENTRYPOINT](https://swe.quest/articles/docker-best-practices-choosing-between-run-cmd-and-entrypoint-023cc3644bb13e6a.md): Docker · 2024-07-15 · Medium · 13 min read · Containers, Linux
  RUN 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.
- [Introducing Docker Build Checks: Optimize Dockerfiles with Best Practices](https://swe.quest/articles/introducing-docker-build-checks-optimize-dockerfiles-with-best-practices-2400d0a2e13e5604.md): Docker · 2024-07-29 · Medium · 6 min read · Containers, Static analysis, CI/CD, Developer tools
  Docker Desktop 4.33 adds Build checks, which evaluate Dockerfiles against codified best-practice rules during \`docker build\` and report warnings.
- [Powered by Docker: Streamlining Engineering Operations as a Platform Engineer](https://swe.quest/articles/powered-by-docker-streamlining-engineering-operations-as-a-platform-engineer-f63ffa3b322e1c4b.md): Docker · 2025-02-18 · Medium · 7 min read · CI/CD, Containers, Observability, Deployment, Infrastructure as code
  Replacing 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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
