Home > other >  ERR_CONNECTION_REFUSED occurs when accessing a public IP address in EC2
ERR_CONNECTION_REFUSED occurs when accessing a public IP address in EC2

Time:02-15

I have created an infrastructure (application) using AWS, Docker (docker-compose), and Rails.
After launching the container in EC2 and starting the rails server, I get "ERR_CONNECTION_REFUSED" when I access the public IP address.
I would like to know how to display the application screen.

My directory structure looks like:

kthr01/
  docker-compose.yml
  Dockerfile
  start.sh
  src/
    app/
     bin/
     ....

Dockerfile

FROM ruby:2.7

ENV RAILS_ENV=production

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
  && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
  && apt-get update -qq \
  && apt-get install -y nodejs yarn \
  && apt-get install -y vim
WORKDIR /app
COPY ./src /app
RUN bundle config --local set path 'vendor/bundle' \
  && bundle install

COPY start.sh /start.sh
RUN chmod 744 /start.sh
CMD ["sh", "/start.sh"]

docker-compose.yml

version: '3'
services:
  db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./src/db/mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

start.sh

#!/bin/sh

if [ "${RAILS_ENV}" = "production" ]
then
    bundle exec rails assets:precompile
fi
bundle exec rails s -p ${PORT:-3000} -b 0.0.0.0

I have a screen shot of the AWS console showing the EC2 security group setup: enter image description here

To run this:

Verify that the container is created in the local environment and appears correctly on localhost:3000

Connect to EC2 via ssh

git clone

docker-compose build

docker-compose up
...
web_1  | => Booting Puma
web_1  | => Rails 6.1.4.4 application starting in production
web_1  | => Run `bin/rails server --help` for more startup options
web_1  | Puma starting in single mode...
web_1  | * Puma version: 5.6.1 (ruby 2.7.5-p203) ("Birdie's Version")
web_1  | *  Min threads: 5
web_1  | *  Max threads: 5
web_1  | *  Environment: production
web_1  | *          PID: 1
web_1  | * Listening on http://0.0.0.0:3000
web_1  | Use Ctrl-C to stop


Access the public IP address of EC2

ERR_CONNECTION_REFUSED is displayed

CodePudding user response:

Based on the comments.

The issue was blocked port 3000 in a security group. The solution was to allow that port.

  • Related