Cant connect to rails docker container on localhost

Viewed 1459

Im having trouble accessing my containerized rails app from my local machine. I'm following this quickstart guide as a template and made some tweaks to the paths for my gemfile and gemfile.lock. The quickstart guide moves on to docker-compose, but I want to try accessing the app without it first to get familiar with these processes before moving on.

This is my dockerfile:

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock
RUN gem install bundler -v 2.0.1
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000:3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

and this is the entrypoint file:

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

I am able to successfully build and run the image, but when I try to access 0.0.0.0:3000 I get a cant connect error. enter image description here

I also attached a screenshot of my app directory structure, the Dockerfile and entrypoint are at the root.

One thing that seems strange is when I try to run the logs for the container I dont get any output, but when I shut the container down I see the startup logs. Not sure why that is.

I am running docker desktop 2.1.0.3. Any thoughts/help are very appreciated.

1 Answers

use just EXPOSE 3000 in dockerfile. run container ror with mapping port to localhost from your new docker image <image>

docker run -d --name ror -p 3000:3000 <image>

now you should be able to access localhost:3000

Related