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

First Post!

henriquesati

henriquesati

no idea why my application on containers were giving me those weird debug messages, but I ran the app on a normal WSL2 ubuntu and could check the error

: connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.467.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

the network mapping was corretly defined (postgres running on localhost ports and mapped) but my config.ex connection port (5431) was being override by connection port on dev.ex (5432)

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.

Last Post!

henriquesati

henriquesati

exactly, i’m trying to make base level config to deploy in any ec2 with docker compose, this without any dependency using or being build by wsl2, only contaieners
problem is the dev-env needs the app to connect to the postgres when I compose (what its not happening)

Where Next?

Popular in Questions Top

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement