set host for rackup puma correctly for docker

1k Views Asked by At

How do you force rackup to use port 0.0.0.0 so it can communicate with your local environment from inside a docker container? Here's my code:

# docker-compose.yml

version: '3'
services:
  backend:
    build: ./ruby-backend
    ports:
      - "4567:4567"

# ruby-backend/Dockerfile

FROM ruby:2.4.1
COPY Gemfile /app/Gemfile
WORKDIR /app
RUN bundle install
COPY . /app
EXPOSE 4567
CMD ["bundle", "exec", "rackup", "-p", "4567", "--host", "0.0.0.0"

This command works great if I run it from outside the container. Here's the terminal output when I do it from my local:

$ bundle exec rackup -s puma -p 4567 -o "0.0.0.0"
Puma starting in single mode...
* Version 3.11.0 (ruby 2.4.1-p111), codename: Love Song
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:4567
Use Ctrl-C to stop

Note the correct host: tcp://0.0.0.0:4567. Unfortunately, it doesn't work like that in the container. Here's the terminal output when I do it using docker:

$ dc up backend
Starting devopscourse1_backend_1 ...
Starting devopscourse1_backend_1 ... done
Attaching to devopscourse1_backend_1
backend_1   | Puma starting in single mode...
backend_1   | * Version 3.11.0 (ruby 2.4.1-p111), codename: Love Song
backend_1   | * Min threads: 0, max threads: 16
backend_1   | * Environment: development
backend_1   | * Listening on tcp://localhost:4567
backend_1   | Use Ctrl-C to stop

Any ideas what I'm doing wrong?

1

There are 1 best solutions below

0
On

I spent the evening debugging this myself and wanted to chime in with the solution I found if you had not already figured it out.

What happens if you run an interactive session with the container?

docker run -t -i <image> /bin/bash

this will give you an interactive shell within the container, that you can then look around the state of the filesystem within the container

I ended up figuring out that the Dockerfile in use by the container had not received the changes I had made to it re: binding 0.0.0.0 to the rackup command. Which reminded me that the Dockerfile, while caching all the setup steps for the process, is itself not part of the container setup. The first instruction copied to the Docker daemon for an image, so changing it means you need to build a new image.

solution:

  1. docker rm the container
  2. docker rmi the image
  3. docker build the image again with the Dockerfile changes to CMD