Skip to content

Instantly share code, notes, and snippets.

@caike
Last active August 10, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/ca6d82c4b47093f3c2b9ac5403268e5f to your computer and use it in GitHub Desktop.
Save caike/ca6d82c4b47093f3c2b9ac5403268e5f to your computer and use it in GitHub Desktop.
Learn the differences between ENVs and ARGs in Docker
FROM alpine:latest
# 01- ENV set directly in Dockerfile.
# Available during BUILDTIME and RUNTIME.
ENV LOCAL_ENV="hi I am LOCAL_ENV"
RUN echo $LOCAL_ENV
# 02 - ARG sent via command (docker or docker-compose)
# Available during BUILDTIME only.
ARG BUILDTIME_ENV
RUN echo $BUILDTIME_ENV
COPY script.sh ./script.sh
CMD ./script.sh
version: "2"
services:
app:
build:
context: .
dockerfile: Dockerfile
# 03 - ARG injected into _building_ container.
# Available during BUILDTIME only.
args:
- BUILDTIME_ENV="I am buildtime env"
environment:
# 04 - ENV Injected into _running_ container.
# Available during RUNTIME only.
- RUNTIME_ENV="I am runtime env"
#!/bin/ash
echo $LOCAL_ENV $BUILDTIME_ENV $RUNTIME_ENV
@caike
Copy link
Author

caike commented Aug 10, 2021

Use the following commands to build and run:

docker build -t orlando-elixir .
docker run --rm orlando-elixir
docker build --build-arg BUILDTIME_ENV="hi I am buildtime env" -t orlando-elixir .

is the same as

docker-compose build

and finally

docker run --env RUNTIME_ENV="I am runtime env" --rm orlando-elixir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment