Skip to content

VarunGarasangi/Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 

Repository files navigation

Docker ARG vs ENV โ€“ Usage Scenarios

๐Ÿ”น What is ARG?

  • ARG is used to pass build-time variables
  • Available only during docker build
  • Not available inside running container (unless passed to ENV)

๐Ÿ”น What is ENV?

  • ENV is used to define runtime environment variables
  • Available inside the container after it starts

Real-World Scenarios

1. Dynamic Base Image Version (ARG)

Use ARG when you want to change base image version without modifying Dockerfile

ARG BASE_VERSION=22.04
FROM ubuntu:${BASE_VERSION}

Use Case

Build different versions:

docker build -t app:v1 --build-arg BASE_VERSION=20.04 .
docker build -t app:v2 --build-arg BASE_VERSION=22.04 .

2. Install Packages Dynamically (ARG)

ARG APP=nginx
RUN apt-get update && apt-get install -y ${APP}

Use Case

Install different software using same Dockerfile

3. Application Configuration (ENV)

ENV APP_ENV=production
ENV DB_HOST=localhost

Use Case

Configure app behavior (dev/test/prod

4. Database Connection (ENV)

ENV DB_USER=admin
ENV DB_PASS=password

Use Case

Provide runtime DB configuration

5. Combine ARG + ENV (Best Practice)

ARG APP_ENV=dev
ENV APP_ENV=${APP_ENV}

Use Case

Pass value at build time โ†’ use at runtime

docker build -t app --build-arg APP_ENV=production .

6. Debug vs Production Mode

ARG MODE=prod
ENV MODE=${MODE}

Use Case

Enable debug logs or optimizations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors