Docker-compose elixir phoenix application with postgres error on build

Hi all, I encountering a problem when executing docker-compose up --build

This is the error I get when i do so,

Since it was something to do with postgres, I looked into my dev.exs file to see if I referenced my environment configs right.

And I also made sure that my docker-compose.yml file had spelt the PGDATABASE environment config right.

06%20PM

My repo is on github at: https://github.com/sc4224/maisie-sample/tree/master/maisie_api

Hope someone will be able to offer some insight as to what is going on here with my postgres database and docker

Environment is not set in build, only on run. You need to migrate your database in the entry point.

1 Like

Was able to get it to work with ENTRYPOINT, on my first try. Problem is after I kill the process and do docker-compose up the second time.

I get an error saying database is already up:

api_1       | [debug] QUERY OK source="schema_migrations" db=2.4ms
api_1       | SELECT s0."version"::bigint FROM "schema_migrations" AS s0 FOR UPDATE []
api_1       | [info] Already up
maisie_api_1 exited with code 0 

I’m guessing this has something to do with the container trying to do mix ecto.create when the database already exists. How do I configure it such that these ENTRYPOINT commands execute once?

I do not see an error there.

maisie_api_1 exited with code 0 is the error. If this is not an error, I don’t understand how to get this to work. Have you worked with phoenix, postgres and docker before? If so, do you have a sample repository of how this is done, where the app does not exit every time mix ecto.create is run?

My repo is https://github.com/sc4224/maisie-sample/tree/master/maisie_api i need somebody to point out what is wrong in this dockerfile and a step by step example of how to fix this. I can’t find documentation anywhere on how to get mix ecto.create and mix ecto.migrate to run once

You need to run those on startup, not as part of the docker build process. Docker compose builds everything independently first, and THEN it starts things. This is to say, the database isn’t available while building the image.

However I can’t provide more specific advice because I can’t actually get things to compile. I downloaded your project and ran docker-compose build but I get:

Generated elixir_make app
==> argon2_elixir
could not compile dependency :argon2_elixir, "mix compile" failed. You can recompile this dependency with "mix deps.compile argon2_elixir", update it with "mix deps.update argon2_elixir" or clean it with "mix deps.clean argon2_elixir"
** (Mix) "make" not found in the path. If you have set the MAKE environment variable,
please make sure it is correct.

==> maisie_api
ERROR: Service 'api' failed to build: The command '/bin/sh -c mix compile' returned a non-zero code: 1

Either way though, when using docker compose you generally want some kind of startup.sh script that is the command, and it includes on boot stuff.

I’m on a mobile therefore I can not properly analyse your docker file, but on a first glance: you do not need to install phx_new inside the archive.

Also it is more common in the docker world to have an entrypoint.sh which “orchestrates” the inner workings of the container.

#!/usr/bin/env sh

mix ecto.create
mix ecto.migrate

mix phx.server

Also your image is lacking essential tools needed to build dependencies as @benwilson512 pointed out.

As soon as I’m back at a computer I’ll do a closer look if no-one else did. This has to wait until after the holidays though.

Funny, I’m back at my PC, and I have to admit, I was able to build the image, also subsequent docker-compose up are working fine (at least if postgres starts before the ecto commands are run).


Additional info: You should not volumes: ["./maisie_app:/app"] in the YAML, that totally defeats the purpose of building in docker first.

I need to specify that volume so I can make changes in the code and the changes propagate to the container for hot reload

Then do not use docker, but run on your system.

Believe me, that one will cause you trouble.

1 Like