Report this

What is the reason for this report?

Accessing environment variable inside Dockerfile - App Platform

Posted on October 22, 2021

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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}

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.