4

I've created a docker image and published it to heroku. For it to work properly I need the DATABASE_URL env variable. Postgres is provisioned to the app, and I can heroku config:get the db URL just fine. Yet after all is done I heroku ps:exec into my app, type 'env' and there is no $PORT or $DATABASE_URL, meaning my app cant get the db string.

the steps I took:

Github Action:

    - name: Build and deploy the Docker image
      env: 
        HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
        APP_NAME: myApp
      run: |
        export DATABASE_URL=$(heroku config:get DATABASE_URL --app ${{ env.APP_NAME }})
        docker login --username=_ --password=${{ env.HEROKU_API_KEY }} registry.heroku.com
        docker build --build-arg DATABASE_URL_ARG=$DATABASE_URL -t registry.heroku.com/${{ env.APP_NAME }}/web .
        docker push registry.heroku.com/${{ env.APP_NAME }}/web

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS publish
WORKDIR /src
ARG DATABASE_URL_ARG
ENV DATABASE_URL=$DATABASE_URL_ARG
COPY . .
RUN dotnet tool install dotnet-ef && dotnet publish -c Release -o ./publish

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
ENV DATABASE_URL=$DATABASE_URL_ARG
WORKDIR /app
COPY --from=publish /src/publish .
CMD DATABASE_URL=${DATABASE_URL} dotnet app.dll

The build process is successful and the postgres db is seeded. I don't know if the second ENV is needed, its there as an artifact of me trying to make something work.

So the question is: where are the missing $PORT and $DATABASE_URL and how did I make them disappear?

Ilya Petrov
  • 131
  • 1
  • 5
  • 1
    I'm not sure why this isn't working, but it looks like you're trying to compile your `DATABASE_URL` into your Docker image at built time? That's not a very good idea, as it can change at any time. Why not just read it from the Heroku-provided environment variable at runtime? – ChrisGPT was on strike Jun 21 '20 at 18:59

3 Answers3

1

Use heroku run bash instead of heroku ps:exec

Learned from: https://stackoverflow.com/a/64951959/895245

I had setup DATABASE_URL with the PostgreSQL plugin heroku addons:create heroku-postgresql:hobby-dev, and they show up under "Config Vars" on the web interface.

The app would run fine, the varaibles were present, but heroku ps:exec didn't have them, while heroku run bash did.

heroku run bash is mentioned at https://devcenter.heroku.com/articles/one-off-dynos in a different context, so I think it should be fine to use it.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • This solves my problem when deploying django and migrations are failing to access the postgreSQL database – Sojimaxi Mar 28 '23 at 11:06
0

It seems to me your problem is that an ARG instruction goes out of scope at the end of the build stage where it was defined, so you'd need the ARG defined in the second stage of the build, too:

# ...
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
ARG DATABASE_URL_ARG
ENV DATABASE_URL=$DATABASE_URL_ARG
WORKDIR /app
COPY --from=publish /src/publish .
CMD DATABASE_URL=${DATABASE_URL} dotnet app.dll
Leonardo Dagnino
  • 2,914
  • 7
  • 28
0

Of course, this was a mistake on my part specific to heroku usage.

After you push your docker image to heroku, you run heroku container:release web I haven't done that, so I couldn't see my changes. As per documentation, $DATABASE_URL, and $PORT and added to the environment automatically and are available at runtime.

Ilya Petrov
  • 131
  • 1
  • 5