henriquesati

henriquesati

How to connect elixir phoenix api with docker container?

I have a application that was running on windows communicating with posgres via postgres mappig localhost8080 to 5432 on postgres container
altought now I need to run everything on linux container, and I cant get to connect my api with postgres even if they were working before, so my question is
How can I do them to communicate with each other? How can I put them in the same network and organize commucation?
I need the elixir app to have all ports exposed to test tcp connections, so a simple intern redirect thing wouldnt work
my docker compose file

version: '3.9'

services:
  elixir_app:
    build:
      context: .
      dockerfile: Dockerfile
    deploy:
      resources:
        limits:
          memory: 25GB
          cpus: "12"
        reservations:
          memory: "0"
          cpus: "0"
    stdin_open: true
    tty: true
  postis:
    image: postgres
    container_name: postis
    environment:
      POSTGRES_PASSWORD: 12345
      POSTGRES_USER: pedri
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:C:\Users\henri\Documents

volumes:
  postgres_data:

config

config :testespay, Testespay.Repo,
  log: false,
  database: "teste10",
  user: "pedri",
  password: "12345",
  hostname: "localhost",
  port: 5432

when I try to run this app container, I get the error

14:17:33.153 [error] Failure while translating Erlang's logger event
** (MatchError) no match of right hand side value: %{label: {:gen_statem, :terminate}, name: #PID<0.507.0>, reason: {:error, %KeyError{key: :username, term: [pool_index: 8, types: Postgrex.DefaultTypes, pool: DBConnection.ConnectionPool, repo: Testespay.Repo, telemetry_prefix: [:testespay, :repo], otp_app: :testespay, timeout: 15000, pool_size: 10, log: false, database: "teste10", user: "pedri", password: "12345", hostname: "localhost", port: 5431, show_sensitive_data_on_connection_error: true], message: nil}, [{Keyword, :fetch!, 2, [file: ~c"lib/keyword.ex", line: 602]}, {Postgrex.Protocol, :startup, 2, [file: ~c"lib/postgrex/protocol.ex", line: 833]}, {Postgrex.Protocol, :handshake, 3, [file: ~c"lib/postgrex/protocol.ex", line: 729]}, {Postgrex.Protocol, :connect_endpoints, 6, [file: ~c"lib/postgrex/protocol.ex", line: 209]}, {DBConnection.Connection, :handle_event, 4, [file: ~c"lib/db_connection/connection.ex", line: 74]}, {:gen_statem, :loop_state_callback, 11, [file: ~c"gen_statem.erl", line: 1397]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, log: [], state: Postgrex.Protocol, queue: [internal: {:connect, :init}], modules: [DBConnection.Connection], client_info: :undefined, callback_mode: :handle_event_function, postponed: [], timeouts: {0, []}, state_enter: false}
    (logger 1.17.0) lib/logger/translator.ex:306: Logger.Translator.report_gen_statem_terminate/2
    (logger 1.17.0) lib/logger/utils.ex:47: Logger.Utils.translate/5
    (logger 1.17.0) lib/logger/utils.ex:25: Logger.Utils.translator/2
    (kernel 9.2.2) logger_backend.erl:101: :logger_backend.do_apply_filters/4
    (kernel 9.2.2) logger_backend.erl:86: :logger_backend.apply_filters/4
    (kernel 9.2.2) logger_backend.erl:32: :logger_backend.log_allowed/3
    (stdlib 5.2.1) gen_statem.erl:2591: :gen_statem.terminate/7
    (stdlib 5.2.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

14:17:33.150 [error] Failure while translating Erlang's logger event
** (MatchError) no match of right hand side value: %{label: {:gen_statem, :terminate}, name: #PID<0.509.0>, reason: {:error, %KeyError{key: :username, term: [pool_index: 2, types: Postgrex.DefaultTypes, pool: DBConnection.ConnectionPool, repo: Testespay.Repo, telemetry_prefix: [:testespay, :repo], otp_app: :testespay, timeout: 15000, pool_size: 10, log: false, database: "teste10", user: "pedri", password: "12345", hostname: "localhost", port: 5431, show_sensitive_data_on_connection_error: true], message: nil}, [{Keyword, :fetch!, 2, [file: ~c"lib/keyword.ex", line: 602]}, {Postgrex.Protocol, :startup, 2, [file: ~c"lib/postgrex/protocol.ex", line: 833]}, {Postgrex.Protocol, :handshake, 3, [file: ~c"lib/postgrex/protocol.ex", line: 729]}, {Postgrex.Protocol, :connect_endpoints, 6, [file: ~c"lib/postgrex/protocol.ex", line: 209]}, {DBConnection.Connection, :handle_event, 4, [file: ~c"lib/db_connection/connection.ex", line: 74]}, {:gen_statem, :loop_state_callback, 11, [file: ~c"gen_statem.erl", line: 1397]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, log: [], state: Postgrex.Protocol, queue: [internal: {:connect, :init}], modules: [DBConnection.Connection], client_info: :undefined, callback_mode: :handle_event_function, postponed: [], timeouts: {0, []}, state_enter: false}
    (logger 1.17.0) lib/logger/translator.ex:306: Logger.Translator.report_gen_statem_terminate/2
    (logger 1.17.0) lib/logger/utils.ex:47: Logger.Utils.translate/5
    (logger 1.17.0) lib/logger/utils.ex:25: Logger.Utils.translator/2
    (kernel 9.2.2) logger_backend.erl:101: :logger_backend.do_apply_filters/4
    (kernel 9.2.2) logger_backend.erl:86: :logger_backend.apply_filters/4
    (kernel 9.2.2) logger_backend.erl:32: :logger_backend.log_allowed/3
    (stdlib 5.2.1) gen_statem.erl:2591: :gen_statem.terminate/7
    (stdlib 5.2.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

the network info around
1-> the network container
2-> postgres container

"Networks": {
			"host": {
				"IPAMConfig": null,
				"Links": null,
				"Aliases": null,
				"MacAddress": "",
				"DriverOpts": null,
				"NetworkID": "1ea463e456351180d5d16950cc6c451dc7a64e9a030901f04c141f9f49808176",
				"EndpointID": "41f016f6a0ac589e0dfa29f0e0c371fe055797edf2e24e7f013430406a1930c4",
				"Gateway": "",
				"IPAddress": "",
				"IPPrefixLen": 0,
				"IPv6Gateway": "",
				"GlobalIPv6Address": "",
				"GlobalIPv6PrefixLen": 0,
				"DNSNames": null
			}
"Networks": {
			"fpay_default": {
				"IPAMConfig": null,
				"Links": null,
				"Aliases": [
					"postiscompose",
					"postiscompose"
				],
				"MacAddress": "02:42:ac:12:00:02",
				"DriverOpts": null,
				"NetworkID": "7b5a170b04b96013cf9f2ddc635cd64af162dc58bef6c078a2e3b6a52e468083",
				"EndpointID": "0fe7f9fbc7aa503bd4e8fc64cd8241f688b3873765a82cc7a8e0c73520657ca1",
				"Gateway": "172.18.0.1",
				"IPAddress": "172.18.0.2",

so I tried running the app with a docker alone postgres (only with images and not compose anymore)
this is the network result
container app

"Networks": {
			"bridge": {
				"IPAMConfig": null,
				"Links": null,
				"Aliases": null,
				"MacAddress": "02:42:ac:11:00:03",
				"DriverOpts": null,
				"NetworkID": "a7f2086d19490cea6832395ef97ccb4b05f31bea753bcc4cefbaa4c10ca36b3a",
				"EndpointID": "b684a7e8e50f3a302f08fca7db7ad9a69bbff62f1aaedb3d18a5c8785f392f49",
				"Gateway": "172.17.0.1",
				"IPAddress": "172.17.0.3",
				"IPPrefixLen": 16,
				"IPv6Gateway": "",
				"GlobalIPv6Address": "",
				"GlobalIPv6PrefixLen": 0,
				"DNSNames": null
			}

postgres

"Networks": {
			"bridge": {
				"IPAMConfig": null,
				"Links": null,
				"Aliases": null,
				"MacAddress": "02:42:ac:11:00:02",
				"DriverOpts": null,
				"NetworkID": "a7f2086d19490cea6832395ef97ccb4b05f31bea753bcc4cefbaa4c10ca36b3a",
				"EndpointID": "67c5e450a0b3cadf645b1c458bbc72f8e6325b6e3bec340d1b5b5905d9368313",
				"Gateway": "172.17.0.1",
				"IPAddress": "172.17.0.2",
				"IPPrefixLen": 16,
				"IPv6Gateway": "",
				"GlobalIPv6Address": "",
				"GlobalIPv6PrefixLen": 0,

I mapped postgres to 5431:5432 on host, and modified my config arquive to connect to localhost:5432

with this second configuration (2 stadalone containers instead of compose)
Im abble to connect to db, but I got timeout, apparently the connections doesnt close itselt, bcs I never had this error when runnig this application before

[error] Postgrex.Protocol (#PID<0.463.0>) timed out because it was handshaking for longer than 15000ms
[error] Postgrex.Protocol (#PID<0.463.0>) failed to connect: ** (DBConnection.ConnectionError) tcp recv (idle): closed
[error] Postgrex.Protocol (#PID<0.464.0>) timed out because it was handshaking for longer than 15000ms
[error] Postgrex.Protocol (#PID<0.464.0>) failed to connect: ** (DBConnection.ConnectionError) tcp recv (idle): closed
[error] Postgrex.Protocol (#PID<0.462.0>) timed out because it was handshaking for longer than 15000ms
[error] Postgrex.Protocol (#PID<0.462.0>) failed to connect: ** (DBConnection.ConnectionError) tcp recv (idle): closed
[error] Postgrex.Protocol (#PID<0.469.0>) timed out because it was handshaking for longer than 15000ms
[error] Postgrex.Protocol (#PID<0.469.0>) failed to connect: ** (DBConnection.ConnectionError) tcp recv (idle): closed
[error] Postgrex.Protocol (#PID<0.470.0>) timed out because it was handshaking for longer than 15000ms

Most Liked

Stefano1990

Stefano1990

You need to use postis as your hostname not localhost.

arcanemachine

arcanemachine

You need to use postis as your hostname not localhost.

Not with their current config.

Using the ports mapping 5432:5432 will expose the service at port 5432 on the host, so localhost is correct in that case.

If the ports mapping was just 5432, then you would be correct.

@henriquesati Are you using the Dockerfile that comes with Phoenix? Because if you are, then your settings are probably being loaded from prod.exs (among others) since IIRC the default Dockerfile builds a production-ready container. So the config would loaded in this order: config.exsprod.exsruntime.exs (the later files overwrite any settings in the earlier files).

IMO, these DB/repo settings should be in runtime.exs 100% of the time anyway. But that’s a whole other can of worms.

dimitarvp

dimitarvp

Sure but… WSL2. I heard stories about Docker not always doing what it’s supposed to do in this environment. I’d still stick to the host names inside the docker-compose.yml file.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement