[SOLVED] Can't access Rails app container's port

Hello,

I cloned the railsgoat project, and edited the Dockerfile by adding EXPOSE 3000 to it.

Then I built it:

docker build -t mccabe615/railsgoat .

I tried various combinations by running the container, for example with:

docker run -it --name goat -p 3000:3000 mccabe615/railsgoat

But couldn’t access this app through localhost:3000. (I also tried different ports, but no luck)

Is there anything wrong with my commands? Thanks!

So just checking that your Docker file looks like something like

FROM rails:onbuild
EXPOSE 3000
MAINTAINER mccabe615
ADD script/start /start
RUN chmod a+x /start
USER root
ENV RAILS_ENV development
CMD /start

Because it looks to me like the app is not copied into the container, i not sure how the original author intended this to work, as it appears like you need to mount the source into this container for this to work. but even then the start script won’t work as it looking for things that won’t be in the same place

Simple test would be to jump inside your container and see if the application is actually running. but using the command docker exec -it goat /bin/bash I suspect your container is starting up then shutting down as it doesn’t have any application to run.

1 Like

Actually we can see that

FROM rails:onbuild

does copy the app (The build will COPY . /usr/src/app, RUN bundle install, EXPOSE 3000, and set the default command to rails server.)
Indeed the build runs fine, and when running the container, I can see that the app has started.

It’s looks like something’s blocking the access to container’s 3000 port. I’m trying to understand, any suggestions are welcome :slight_smile:

I think it is not about the port. It is the IP, docker/conatiners have own IP’s
Try this to get the IP,
$ docker-machine ip default

If you are using docker toolbox, open Kitematic and select the container which you want, go to Settings > Port.

There you can get the IP and Port for that particular container.
Hope this works for you.

edit: Since you have already exposed your port through -p 3000:3000, through docker_ip:3000 you can access the app.

Ok learnt something new about the rails docker container! Awesome.

No sure if @siddharth67 suggestion considered that you are already exposing the port on your host using the -p flag.

Having a bit more a poke around the github repo i noticed by default the app starts up listening on 127.0.0.1:3000, since it’s not listening on the interface exposed in the container it will never get and traffic set to it from the host. if you change this to 0.0.0.0:3000 i think everything will work as you expect

1 Like

@timgriffiths Ahh, you are correct! For Rails app this means that I needed to start the server with -b switch, so I edited the

script/start

and changed

rails server

to:

rails server -b 0.0.0.0

And I can access the app!

Thank you guys!

2 Likes