Jakub Zak
Jakub Zak

Reputation: 1232

Exposing/Publishing ports from docker

I have a small REST API running in the docker container:

[2015-10-15 10:47:05] INFO  WEBrick 1.3.1
[2015-10-15 10:47:05] INFO  ruby 2.2.3 (2015-08-18) [x86_64-linux]
[2015-10-15 10:47:05] INFO  WEBrick::HTTPServer#start: pid=8 port=9292

I have docker VM on IP:

192.168.99.100

I am trying to publish/expose my ports:

docker run --publish 9292:9292 788a36b503e2

Docker ps is showing:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9d6b34469ebf        788a36b503e2        "/bin/sh -c 'rackup -"   45 seconds ago      Up 44 seconds       0.0.0.0:9292->9292/tcp   hopeful_bhaskara

And I am trying to hit the REST API:

 curl 192.168.99.100:9292/auth/3/xyz

But still getting error:

 curl: (7) Failed to connect to 192.168.99.100 port 9292: Connection refused

Any idea why? Thanks

Upvotes: 0

Views: 675

Answers (5)

Jakub Zak
Jakub Zak

Reputation: 1232

At the end of the day I never found out what I was doing wrong. I took a different approach to the problem and solved it by using Forman and Thin.

My Dockerfile:

FROM buildpack-deps:jessie

ENV RUBY_MAJOR 2.2
ENV RUBY_VERSION 2.2.3
ENV RUBY_DOWNLOAD_SHA256 df795f2f99860745a416092a4004b016ccf77e8b82dec956b120f18bdc71edce
ENV RUBYGEMS_VERSION 2.4.8

# skip installing gem documentation
RUN echo 'install: --no-document\nupdate: --no-document' >> "$HOME/.gemrc"

# some of ruby's build scripts are written in ruby
# we purge this later to make sure our final image uses what we just built
RUN apt-get update \
    && apt-get install -y bison libgdbm-dev ruby \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /usr/src/ruby \
    && curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
    && echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.gz" | sha256sum -c - \
    && tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    && rm ruby.tar.gz \
    && cd /usr/src/ruby \
    && autoconf \
    && ./configure --disable-install-doc \
    && make -j"$(nproc)" \
    && make install \
    && apt-get purge -y --auto-remove bison libgdbm-dev ruby \
    && gem update --system $RUBYGEMS_VERSION \
    && rm -r /usr/src/ruby

# install things globally, for great justice
ENV GEM_HOME /usr/local/bundle
ENV PATH $GEM_HOME/bin:$PATH

ENV BUNDLER_VERSION 1.10.6

RUN gem install bundler --version "$BUNDLER_VERSION" \
    && bundle config --global path "$GEM_HOME" \
    && bundle config --global bin "$GEM_HOME/bin"

# don't create ".bundle" in all our apps
ENV BUNDLE_APP_CONFIG $GEM_HOME

COPY app /auth_app

WORKDIR /auth_app

RUN bundle install

CMD ["foreman","start","-d","/auth_app"]

EXPOSE 4567

My Gemfile:

source 'https://rubygems.org'

gem 'sinatra'
gem 'foreman'
gem 'nori'
gem 'thin'
gem 'rack'
gem 'rack-test'

My Procfile:

web: bundle exec rackup --env dev config.ru -p 4567 -s thin -o 0.0.0.0

My docker command running it:

docker run -p 4567:4567 -d auth_app

I didn't change any code within the app, just the docker deployment setup, it worked like a charm then. Thank you for all your answers, they helped me at least to concentrate onto right problems and ask right questions to get this running.

Upvotes: 1

bdparrish
bdparrish

Reputation: 2764

What does your Dockerfile look like?

You have to EXPOSE the port to be seen by the -p flag in the docker run command.

Upvotes: 0

Emre Can Yılmaz
Emre Can Yılmaz

Reputation: 116

You said that:

I have docker VM on IP:

192.168.99.100

If you are using VirtualBox for VM and you want to access this url(http://localhost:9292) on your local machine, you need to do port forwarding on VirtualBox for access the VM.

Warning: If your VM has firewall(such as ufw), disable it. (sudo ufw disable)

Follow the steps below for port forwarding.

  1. Open the VirtualBox GUI on your local machine.

  2. Find your VM in the list of virtual machines and select Settings from the top nav

  3. Click on the Network icon on the top nav

  4. Select Port Forwarding

  5. Click the Add rule icon on the top right and fill in your Host port, 9292, and your Guest port, 9292

  6. Click OK and OK and the forward is set.

Now, you can access to http://localhost:9292 on your local machine.

Upvotes: 4

Manuel J. Garrido
Manuel J. Garrido

Reputation: 2414

If you are running

docker run --publish 9292:9292 788a36b503e2

you are binding the 9292 container's port with that same port on the host. So running

 curl localhost:9292/auth/3/xyz

from the host should work.

Upvotes: 0

Eitan
Eitan

Reputation: 128


A bit more info is needed -
Can you curl localhost:9292 from within the container?
If so, from the host running the container, can you curl the URL (curl 192.168.99.100:9292)?
If so, the REST server might be off or wrongly configured.

Are you using a standard image (d/l from Docker hub)? if so - please send a link so it would be possible to recreate.

Upvotes: 3

Related Questions