简体   繁体   中英

Elixir and Docker

I have a freshly generated Phoenix app and I'm trying to Dockerize it.

I'm running into the following issue: If I boot Phoenix locally ( mix phx.server ) it works fine (connects to localhost PG) If I run it in PROD ( MIX_ENV=prod mix phx.server ) it connects to RDS

If I run in Docker / dev mode ( docker-compose up ) I get this connection error 在此输入图像描述

If I run in Docker / prod mode I get the same error

I figure Docker not connecting locally is due to some networking issue (localhost vs docker host), but it still looks for localhost when running in prod.

Tried:

`docker-compose -f docker-compose.yml -f docker-compose.prod.yml up`
`MIX_ENV=prod docker-compose -f docker-compose.yml -f docker-compose.prod.yml up`

I've tried Phoenix 1.3 and the latest RCs with no luck.

Elixir does ask for a DATABASE_URL on top of the normal prod.secret.exs DB config

I even tried putting a PG container into the compose setup and linked it. Elixir still asks for Localhost.

No DATABASE_URL in my env.

I've tried umbrella apps all the way to vanilla Phoenix. No love at all.

I have exported PORT (as Elixir wants that)

You have to tell Pheonix which host to access your database on. That is not localhost inside the container- Postgres is running in a different container. From your screenshot looks like you named your db container postgres which means you can hit your db at http://postgres from inside your Phoenix container. Update your connection details for local to use postgres or set the DATABASE_URL=postgres as an environment variable in your docker-compose.yml file.

If I run in Docker / dev mode (docker-compose up) I get this connection error

mix phx.server will use dev config as default, so which means if you run the image with dev mode, it will use the dev config, the DB hostname will be localhost .

If I run in Docker / prod mode I get the same error

Usually, phoenix generates the prod.secret.exs does not have hostname , you need to add it manually. If you run it against docker-compose , the hostname should be the database service name you specified in your docker-compose.yml

No DATABASE_URL in my env.

If you try to use DATABASE_URL format configuration, you need to add this environment variable into your docker-compose.yml backend service configuration. In general, if you use distillery , you can add REPLACE_OS_VARS in your rel/config.exs , then you can use the environment variable like: ${DATABASE_URL} .

  1. REPLACE_OS_VARS only works with strings
  2. you can refer this repository to try it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM