How to access configuration variables during Docker build
By design (for better or worse), Docker doesn’t allow setting arbitrary environment variables during the Docker build process: that is only possible when running Containers after the Image is built.
The rationale for this is that Dockerfiles should be fully portable and not tied to any specific environment.
A direct consequence of this design is that your Configuration variables, set via aptible config:set
, are not available to commands executed during the Docker build.
It’s a good idea to follow Docker best practice and avoid depending on Configuration variables in instructions in your Dockerfile, but if you absolutely need to, Aptible provides a workaround: .aptible.env
.
.aptible.env
When building your image, Aptible injects a .aptible.env
file at the root of your repository prior to running the Docker build. The file contains your Configuration variables, and can be sourced by a shell.
Here’s an example:
RAILS_ENV=production
DATABASE_URL=postgresql://user:password@host:123/db
If needed, you can use this file to access environment variables during your build, like this:
# Assume that you've already ADDed your repo:
ADD . /app
WORKDIR /app
# The bundle exec rake assets:precompile command
# will run with your configuration
RUN set -a && . /app/.aptible.env && \
bundle exec rake assets:precompile
❗️ Do not use the
.aptible.env
file outside of Dockerfile instructions. This file is only injected when your image is built, so changes to your configuration will not be reflected in the.aptible.env
file unless you deploy again or rebuild. Outside of your Dockerfile, your configuration variables are accessible in the Container Environment.
Was this page helpful?