Configuring app to listen on a specific address

Hi, running into a bug where my rails app is deployed but the content of my app is unreachable:

WARNING The app is not listening on the expected address and will not be reachable by fly-proxy.
You can fix this by configuring your app to listen on the following addresses:
  - 0.0.0.0:3000
Found these processes inside the machine with open listening sockets:
  PROCESS        | ADDRESSES                            
-----------------*--------------------------------------
  /goapp/app     | [::]:8080                            
  /.fly/hallpass | [fdaa:2:da27:a7b:93:3663:cc19:2]:22

I’ve used similar topics to figure out that I needed to update my fly.toml and Dockerfile to use the default Rails port of 3000.

# fly.toml app configuration file generated for alex-bunty on 2023-08-25T13:16:32-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "alex-bunty"
primary_region = "iad"

[build]
  image = "flyio/hellofly:latest"

[env]
  PORT = "3000"

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential pkg-config

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libsqlite3-0 && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
    mkdir /data && \
    chown -R rails:rails db log storage tmp /data
USER rails:rails

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
    RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
VOLUME /data
CMD ["./bin/rails", "server -b 0.0.0.0"]

The two types of errors in my logs are:

[error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)
[error] could not find a good candidate within 90 attempts at load balancing

As an additional check I even edited my puma.rb file to bind ‘0.0.0.0’ to my port (just to remove any possible instance of 127.0.0.1). I realize this shouldn’t matter as the puma server is used for the development environment and not production environment, but I just felt I needed to make the change for sanity’s sake. I have no issues running the app in the local development environment.

I was reading about the arguments for CMD in the Dockerfile and thought that maybe the command “server” was starting up the unreachable 127.0.0.1:3000, so I attempted to change that by using the command "server -b 0.0.0.0” to bind 0.0.0.0 to the port (though from my reading it seems that Docker defaults to 0.0.0.0 and people usually have the opposite issue). Still receiving the same result after making this change.

Is there somewhere else in my codebase I should inspect to configure my app to listen on 0.0.0.0:3000 ?

This is my entire logs:

2023-08-25T17:16:39.232 runner[148e1e7a322928] iad [info] Pulling container image registry-1.docker.io/flyio/hellofly:latest

2023-08-25T17:16:39.356 runner[148e1e7a322928] iad [info] Successfully prepared image registry-1.docker.io/flyio/hellofly:latest (124.627483ms)

2023-08-25T17:16:39.915 runner[148e1e7a322928] iad [info] Configuring firecracker

2023-08-25T17:16:40.367 app[148e1e7a322928] iad [info] [ 0.049135] PCI: Fatal: No config space access function found

2023-08-25T17:16:40.582 app[148e1e7a322928] iad [info] INFO Starting init (commit: b437b5b)...

2023-08-25T17:16:40.660 app[148e1e7a322928] iad [info] INFO Preparing to run: `/goapp/app` as root

2023-08-25T17:16:40.664 app[148e1e7a322928] iad [info] INFO [fly api proxy] listening at /.fly/api

2023-08-25T17:16:40.751 app[148e1e7a322928] iad [info] 2023/08/25 17:16:40 listening on [fdaa:2:da27:a7b:93:3663:cc19:2]:22 (DNS: [fdaa::3]:53)

2023-08-25T17:16:41.098 app[148e1e7a322928] iad [info] [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

2023-08-25T17:16:41.098 app[148e1e7a322928] iad [info] [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

2023-08-25T17:16:41.098 app[148e1e7a322928] iad [info] - using env: export GIN_MODE=release

2023-08-25T17:16:41.098 app[148e1e7a322928] iad [info] - using code: gin.SetMode(gin.ReleaseMode)

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] [GIN-debug] Loaded HTML Templates (2):

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] -

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] - hellofly.tmpl

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] [GIN-debug] GET / --> main.handleIndex (3 handlers)

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] [GIN-debug] GET /:name --> main.handleIndex (3 handlers)

2023-08-25T17:16:41.100 app[148e1e7a322928] iad [info] [GIN-debug] Listening and serving HTTP on :8080

2023-08-25T17:16:51.718 runner[e286d20b934428] iad [info] Pulling container image registry-1.docker.io/flyio/hellofly:latest

2023-08-25T17:16:51.899 runner[e286d20b934428] iad [info] Successfully prepared image registry-1.docker.io/flyio/hellofly:latest (180.546361ms)

2023-08-25T17:16:52.449 runner[e286d20b934428] iad [info] Configuring firecracker

2023-08-25T17:16:52.780 app[e286d20b934428] iad [info] [ 0.042174] PCI: Fatal: No config space access function found

2023-08-25T17:16:52.987 app[e286d20b934428] iad [info] INFO Starting init (commit: b437b5b)...

2023-08-25T17:16:53.042 app[e286d20b934428] iad [info] INFO Preparing to run: `/goapp/app` as root

2023-08-25T17:16:53.047 app[e286d20b934428] iad [info] INFO [fly api proxy] listening at /.fly/api

2023-08-25T17:16:53.124 app[e286d20b934428] iad [info] 2023/08/25 17:16:53 listening on [fdaa:2:da27:a7b:92:9cee:ee75:2]:22 (DNS: [fdaa::3]:53)

2023-08-25T17:16:53.403 app[e286d20b934428] iad [info] [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

2023-08-25T17:16:53.403 app[e286d20b934428] iad [info] [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

2023-08-25T17:16:53.403 app[e286d20b934428] iad [info] - using env: export GIN_MODE=release

2023-08-25T17:16:53.403 app[e286d20b934428] iad [info] - using code: gin.SetMode(gin.ReleaseMode)

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] [GIN-debug] Loaded HTML Templates (2):

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] - hellofly.tmpl

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] -

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] [GIN-debug] GET / --> main.handleIndex (3 handlers)

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] [GIN-debug] GET /:name --> main.handleIndex (3 handlers)

2023-08-25T17:16:53.404 app[e286d20b934428] iad [info] [GIN-debug] Listening and serving HTTP on :8080

2023-08-25T17:17:09.477 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:09.503 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:19.855 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:20.861 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:29.937 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:38.821 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:40.736 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:45.744 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:47.748 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:56.617 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:17:58.623 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:18:07.491 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:18:18.455 proxy[148e1e7a322928] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:18:19.434 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:18:30.419 proxy[e286d20b934428] iad [error] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

2023-08-25T17:18:34.371 proxy[e286d20b934428] iad [error] could not find a good candidate within 90 attempts at load balancing

2023-08-25T17:24:20.533 proxy [148e1e7a322928] iad [info] Downscaling app alex-bunty in region iad from 2 machines to 1 machines. Automatically stopping machine 148e1e7a322928

2023-08-25T17:24:20.539 app[148e1e7a322928] iad [info] INFO Sending signal SIGINT to main child process w/ PID 256

2023-08-25T17:24:21.223 app[148e1e7a322928] iad [info] INFO Main child exited with signal (with signal 'SIGINT', core dumped? false)

2023-08-25T17:24:21.224 app[148e1e7a322928] iad [info] INFO Starting clean up.

2023-08-25T17:24:21.227 app[148e1e7a322928] iad [info] WARN hallpass exited, pid: 257, status: signal: 15 (SIGTERM)

2023-08-25T17:24:21.234 app[148e1e7a322928] iad [info] 2023/08/25 17:24:21 listening on [fdaa:2:da27:a7b:93:3663:cc19:2]:22 (DNS: [fdaa::3]:53)

2023-08-25T17:24:22.226 app[148e1e7a322928] iad [info] [ 461.900368] reboot: Restarting system

2023-08-25T17:26:26.957 proxy [e286d20b934428] iad [info] Downscaling app alex-bunty in region iad from 1 machines to 0 machines. Automatically stopping machine e286d20b934428

2023-08-25T17:26:26.960 app[e286d20b934428] iad [info] INFO Sending signal SIGINT to main child process w/ PID 256

2023-08-25T17:26:27.707 app[e286d20b934428] iad [info] INFO Main child exited with signal (with signal 'SIGINT', core dumped? false)

2023-08-25T17:26:27.708 app[e286d20b934428] iad [info] INFO Starting clean up.

2023-08-25T17:26:27.708 app[e286d20b934428] iad [info] WARN hallpass exited, pid: 257, status: signal: 15 (SIGTERM)

2023-08-25T17:26:27.709 app[e286d20b934428] iad [info] 2023/08/25 17:26:27 listening on [fdaa:2:da27:a7b:92:9cee:ee75:2]:22 (DNS: [fdaa::3]:53)

2023-08-25T17:26:28.703 app[e286d20b934428] iad [info] [ 575.959218] reboot: Restarting system

Try removing those lines. The problem here is that it isn’t running your Dockerfile at all, instead it is running that image.

Thank you! I think that definitely solved my port issues. Now I have a new issue where postgres is failing to install when bundler runs.

#0 11.42 An error occurred while installing pg (1.5.3), and Bundler cannot continue.
#0 11.42 
#0 11.42 In Gemfile:
#0 11.42   pg
Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c bundle install &&     bundle exec bootsnap precompile --gemfile &&     rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git]: exit code: 5

When I run bundle install in my current directory, all the Gemfile dependencies (including pg) all install with no issue. I only seem to have the issue when bundle install is running during deployment. Any idea as to why it would encounter the issue during the deployment bundle?

If all is going according to plan, the following should lead you through the correction needed to your Dockerfile:

bin/rails generate dockerfile

If that doesn’t work for you, change your Dockerfile to match:

Thanks for the guidance here! I changed my Dockerfile to match those lines. After making that change, I re-deployed but am now getting a file path error that is causing my app to crash and fail deployment:

 LoadError: libpq.so.5: cannot open shared object file: No such file or directory - /rails/vendor/bundle/ruby/3.2.0/gems/pg-1.5.3/lib/pg_ext.so

I couldn’t find any similar topics in Fly’s community. I’ve scoured other communities as well and I’ve taken steps recommended there but the problem persists. I’ve ensured that postgresql and libpq are installed on my computer. I updated my .zsrhc file per Homebrew’s recommendations when I installed postgresql (see last three lines):

export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
export GEM_HOME=/usr/local/opt/ruby/lib/ruby/gems/3.2.2/bin
export GEM_PATH=/usr/local/opt/ruby/lib/ruby/gems/3.2.2/bin
export LDFLAGS="-L/usr/local/opt/libpq/lib"
export CPPFLAGS="-I/usr/local/opt/libpq/include"
export PKG_CONFIG_PATH="/usr/local/opt/libpq/lib/pkgconfig"

Per the recommendations in this post, I used:

find / -name libpq.so.5

which returned:

find: /usr/sbin/authserver: Permission denied

It’s strange to me that the returned path didn’t include /libpq.so.5 . Do you have any recommendations on how I can update my path so that libpq can be accessed?

The load error you are seeing is happening on fly.io’s virtual machine, not on your desktop/laptop. Which would indicate that your Dockerfile still isn’t correct. If you run bin/rails generate dockerfile again, does it show any differences?

Another thing to try: https://github.com/fly-apps/dockerfile-rails/blob/main/DEMO.md#demo-3---action-cable-and-active-record ; if you have docker installed locally you can test this locally before deplying it to fly.io. If you don’t have docker installed locally, feel free to skip the docker compose build and docker compose up steps and run fly launch and fly deploy from there. Be sure to say yes to both postgres and redis.

Ahh ok thank you for that clarification about the load error. I ran bin/rails generate dockerfile again and there is one difference (‘pkg-config’ is a new addition):

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential libpq-dev pkg-config

I don’t have docker installed locally, but that is good to know that I could test it that way. I may download to play around with it.

That last suggestion was indeed the solution and I was able to successfully deploy. As a side note - I ran into the “app already contains a secret named DATABASE_URL” error along the way and used this topic to work through it, in case it is helpful to other users.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.