How to run database migrations?

In my Dockerfile, at the very end, I want to run the database migrations, then run the app server.
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/nuki ./

USER nobody

CMD ["/app/bin/migrate"]
CMD ["/app/bin/server"]
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/nuki ./

USER nobody

CMD ["/app/bin/migrate"]
CMD ["/app/bin/server"]
But it seems like my migrations are never executed. In the Build Logs I see:
#28 [stage-1 6/6] COPY --from=builder --chown=nobody:root /app/_build/prod/rel/nuki ./
#28 sha256:682213f68da1d2ab828e67a48b1dbe5877bcef4cc00e010e13656bf08da19357

#28 DONE 0.4s


#29 exporting to image
#29 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#29 exporting layers

#29 exporting layers 0.3s done
#29 writing image sha256:56c5a0f8bb77edd876bd3a34abf8989977de81ee815883dd285a2868fbf39a89

#29 writing image sha256:56c5a0f8bb77edd876bd3a34abf8989977de81ee815883dd285a2868fbf39a89 done
#29 naming to us-west1-docker.pkg.dev/railway-infra/railway-docker-users/project/4c052b8f-000c-44d0-b00a-2b650e36cf40/service/3a4fe599-38ed-432d-9263-66fb1ef9fd29:16762b8d-126e-46fc-9c12-63db8cba8a75 0.0s done
#29 DONE 0.5s

Build time: 21.25 seconds

================
Publishing Image
================
The push refers to repository [us-west1-docker.pkg.dev/railway-infra/railway-docker-users/project/4c052b8f-000c-44d0-b00a-2b650e36cf40/service/3a4fe599-38ed-432d-9263-66fb1ef9fd29]
Preparing 2f3b7ddf1a97
#28 [stage-1 6/6] COPY --from=builder --chown=nobody:root /app/_build/prod/rel/nuki ./
#28 sha256:682213f68da1d2ab828e67a48b1dbe5877bcef4cc00e010e13656bf08da19357

#28 DONE 0.4s


#29 exporting to image
#29 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#29 exporting layers

#29 exporting layers 0.3s done
#29 writing image sha256:56c5a0f8bb77edd876bd3a34abf8989977de81ee815883dd285a2868fbf39a89

#29 writing image sha256:56c5a0f8bb77edd876bd3a34abf8989977de81ee815883dd285a2868fbf39a89 done
#29 naming to us-west1-docker.pkg.dev/railway-infra/railway-docker-users/project/4c052b8f-000c-44d0-b00a-2b650e36cf40/service/3a4fe599-38ed-432d-9263-66fb1ef9fd29:16762b8d-126e-46fc-9c12-63db8cba8a75 0.0s done
#29 DONE 0.5s

Build time: 21.25 seconds

================
Publishing Image
================
The push refers to repository [us-west1-docker.pkg.dev/railway-infra/railway-docker-users/project/4c052b8f-000c-44d0-b00a-2b650e36cf40/service/3a4fe599-38ed-432d-9263-66fb1ef9fd29]
Preparing 2f3b7ddf1a97
And in my Deploy Logs:
21:26:31.624 [info] Running NukiWeb.Endpoint with cowboy 2.9.0 at :::6960 (http)
21:26:31.624 [info] Access NukiWeb.Endpoint at https://nuki-production.up.railway.app
21:26:31.624 [info] Running NukiWeb.Endpoint with cowboy 2.9.0 at :::6960 (http)
21:26:31.624 [info] Access NukiWeb.Endpoint at https://nuki-production.up.railway.app
So that last CMD does seem to be running, why would the one right above it not run? CMD ["/app/bin/migrate"]
18 Replies
slymilano
slymilano2y ago
In the template, you says say to run: railway run mix ecto.migrate but I want the migration to always run as part of the deploy. Any way to accomplish this?
JustJake
JustJake2y ago
I don't think you can do 2 CMDs. How about
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/nuki ./

USER nobody

RUN ["/app/bin/migrate"]
CMD ["/app/bin/server"]
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/nuki ./

USER nobody

RUN ["/app/bin/migrate"]
CMD ["/app/bin/server"]
Finn
Finn2y ago
CMD can be like. Sh migrateAndServer.sh or however you run a sh ? Iirc And just shove the commands in the sh file
JustJake
JustJake2y ago
Only one CMD
slymilano
slymilano2y ago
thank you makes sense. I am getting this missing ENV database_url error.
#28 [stage-1 6/7] COPY --from=builder --chown=nobody:root /app/_build/prod/rel/nuki ./
#28 sha256:1d6dfc3de78e4d6343a47f466891f6e87dade5174bca0fc5100afa2362688b3a
#28 CACHED

#29 [stage-1 7/7] RUN ["/app/bin/migrate"]
#29 sha256:06b3be0f8a2b8c23b92eeda34cb8f613aec71082cb90e1e8c0a264d49ec82e51

#29 0.855 ERROR! Config provider Config.Reader failed with:

#29 0.859 ** (RuntimeError) environment variable DATABASE_URL is missing.
#29 0.859 For example: ecto://USER:PASS@HOST/DATABASE
#29 0.859
#29 0.859 (stdlib 3.17.2) erl_eval.erl:685: :erl_eval.do_apply/6
#29 0.859 (stdlib 3.17.2) erl_eval.erl:446: :erl_eval.expr/5
#28 [stage-1 6/7] COPY --from=builder --chown=nobody:root /app/_build/prod/rel/nuki ./
#28 sha256:1d6dfc3de78e4d6343a47f466891f6e87dade5174bca0fc5100afa2362688b3a
#28 CACHED

#29 [stage-1 7/7] RUN ["/app/bin/migrate"]
#29 sha256:06b3be0f8a2b8c23b92eeda34cb8f613aec71082cb90e1e8c0a264d49ec82e51

#29 0.855 ERROR! Config provider Config.Reader failed with:

#29 0.859 ** (RuntimeError) environment variable DATABASE_URL is missing.
#29 0.859 For example: ecto://USER:PASS@HOST/DATABASE
#29 0.859
#29 0.859 (stdlib 3.17.2) erl_eval.erl:685: :erl_eval.do_apply/6
#29 0.859 (stdlib 3.17.2) erl_eval.erl:446: :erl_eval.expr/5
I added ARG DATABASE_URL to the top of my Dockerfile.
RG ELIXIR_VERSION=1.13.4
ARG OTP_VERSION=24.3.4
ARG DEBIAN_VERSION=bullseye-20210902-slim
ARG DATABASE_URL
RG ELIXIR_VERSION=1.13.4
ARG OTP_VERSION=24.3.4
ARG DEBIAN_VERSION=bullseye-20210902-slim
ARG DATABASE_URL
Finn
Finn2y ago
RUN steps run at builtime afaik, dunno if railway populates env vars at that point
JustJake
JustJake2y ago
We do
Finn
Finn2y ago
Ah oke. All slymalino should need is the second green part then?
slymilano
slymilano2y ago
let me try that
Finn
Finn2y ago
Providing ENV works at builtime. If you get what I mean Otherwise an sh file might do you aswell
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JustJake
JustJake2y ago
^^Or that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
slymilano
slymilano2y ago
Awesome thanks so much guys loving railway
Finn
Finn2y ago
Interesting, should health checks be used when using any docker file with railway?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Finn
Finn2y ago
Ohhhhhh I was typing a follow up question about that. Makes sense. Time to do some research
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View