Question

Accessing environment variable inside Dockerfile - App Platform

Posted October 22, 2021 151 views
DockerDigitalOcean App Platform

Hello,

I am running a website on DigitalOcean App Platform. I have couple of environment variables already configured and working fine. But What I am now trying to do is, I want to access a environment variable inside Dockerfile during build time. My use case is like below:

Dockerfile:

CP ./config/staging/conf.d /etc/nginx/conf.d

As you can see, I am trying to make the staging string into an environment variable so that I can use it on both my staging & production branches.

I tried something like this:

CP ./config/${ENVIRONMENT}/conf.d /etc/nginx/conf.d

But it didn’t work. Can someone help me?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

I figured it out.

We should define our env variable as ARG at the beginning of the Dockerfile like this:

ARG ENVIRONMENT

Then we can access it later with ${ENVIRONMENT}