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
You need to use postis as your hostname not localhost.
arcanemachine
You need to use
postisas your hostname notlocalhost.
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.exs → prod.exs → runtime.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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








