Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #47: Build time vs run time ENV variables

blog/cards/docker-tips-and-tricks.jpg

Docker allows you to set both build time and run time ENV variables and even lets you overwrite build time ENV vars at run time.

There may come a time where you’ll want to add an ENV variable to your Docker image, but you’ll want this value to be different depending on where you build your image, but you don’t want to edit your Dockerfile to change it.

You may also want to sometimes overwrite that value at run time (not build time).

Setting up a Docker image with a build argument and ENV variable:
FROM alpine:3.7

ARG YOURAPP_ENV=production
ENV YOURAPP_ENV="${YOURAPP_ENV}"

YOURAPP_ENV could be RAILS_ENV, NODE_ENV or whatever environment variable you want to set at build time.

The ARG and ENV variable names don’t need to match, but it’s generally a good idea to keep them the same for consistency. In this case, the ENV variable will default to whatever the ARG is set to at build time (or “production” if none is supplied).

A few outcomes when building and running the above Docker image:
# A typical build.
docker build -t yourapp .
docker run --rm yourapp env | grep YOURAPP_ENV
> YOURAPP_ENV=production

# Building it with a custom --build-arg value.
docker build -t yourapp --build-arg YOURAPP_ENV=staging .
docker run --rm yourapp env | grep YOURAPP_ENV
> YOURAPP_ENV=staging

# Overwriting the ARG's value at run time instead of build time.
docker run --rm -e YOURAPP_ENV=development yourapp env | grep YOURAPP_ENV
> YOURAPP_ENV=development

If you’re using Docker Compose instead, it supports both setting build arguments and also setting environment variables at run time, so it works the same way.

A quick Docker Compose example that sets a build argument:
version: "3"

services:
  yourapp:
    build:
      context: "."
      args:
        YOURAPP_ENV: "helloworld"

Now if you ran docker-compose up --build and took a look at the environment variables in the container you would see YOURAPP_ENV=helloworld.

And as expected if you set YOURAPP_ENV as an environment variable in the docker-compose.yml file it would overwrite the build argument just like it did without Docker Compose. Feel free to try that out on your own!

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.


Comments