Docker container sending empty responses
Asked Answered
B

1

16

Hoping someone can tell me what I am missing? This is a ruby app using webrick and I am trying to containerize the app. Running on Mac OSX 10.12.3 Sierra. Here is my Dockerfile

FROM ruby:2.4.0-alpine
RUN apk add --no-cache gcc musl-dev libstdc++ g++ make
RUN gem install jekyll bundler redcarpet
RUN mkdir -p /usr/app/jekyll
COPY . /usr/app/jekyll
WORKDIR /usr/app/jekyll
EXPOSE 4000:4000
CMD ["jekyll", "serve"]

Here is how the image is built

docker build -t chb0docker/cheat .

if I run the service directly on the host, it runs fine

Violas-MacBook-Pro:progfun-wiki cbongiorno$ jekyll serve &
[1] 49286
Violas-MacBook-Pro:progfun-wiki cbongiorno$ Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
            Source: /Users/cbongiorno/development/progfun-wiki
       Destination: /Users/cbongiorno/development/progfun-wiki/_site
 Incremental build: disabled. Enable with --incremental
      Generating... 
                    done in 0.409 seconds.
 Auto-regeneration: enabled for '/Users/cbongiorno/development/progfun-wiki'
Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.

verify the server is up:

Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl localhost:4000 2> /dev/null | wc -l
      40

now run it in docker:

Violas-MacBook-Pro:progfun-wiki cbongiorno$ fg
jekyll serve
^C
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker run -d --name jekyll -p 4000:4000 chb0docker/cheat 
e766e4acb007033583885b1a3c52dc3c2dc51c6929c8466f3a4ff958f76ebc5f

verify the process

Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
e766e4acb007        chb0docker/cheat    "jekyll serve"      19 minutes ago      Up 32 seconds       0.0.0.0:4000->4000/tcp   jekyll
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' jekyll
172.17.0.3
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker logs jekyll
Configuration file: /usr/app/jekyll/_config.yml
Configuration file: /usr/app/jekyll/_config.yml
            Source: /usr/app/jekyll
       Destination: /usr/app/jekyll/_site
 Incremental build: disabled. Enable with --incremental
      Generating... 
                    done in 0.329 seconds.
 Auto-regeneration: enabled for '/usr/app/jekyll'
Configuration file: /usr/app/jekyll/_config.yml
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.

now try and get some data

Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl localhost:4000 2> /dev/null | wc -l
       0
Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl 172.17.0.3:4000 2> /dev/null | wc -l
       0
Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl 0.0.0.0:4000 2> /dev/null | wc -l
       0

but, if we execute the above GET (with wget instead of curl because it's not installed on this container) we can see all is well inside the container

 docker exec -it jekyll /usr/bin/wget -q -O - localhost:4000 | wc -l
      40

Is this an app issue?

Breeden answered 31/1, 2017 at 17:47 Comment(0)
E
28

Looks like Jekyll is binding to localhost. Either start the container like this:

docker run -d --name jekyll -p 127.0.0.1:4000:4000

Or have Jekyll bind to 0.0.0.0:

CMD ["jekyll", "serve", "--host", "0.0.0.0"]
Epact answered 31/1, 2017 at 18:5 Comment(3)
That's exactly what I suspected! The question was "what to do about it" - I went with option 2 as it makes the image completely portable and no one has to know anything extra to run it.Breeden
I'm using Laravel. Setting the Docker port with 127.0.0.1:4000:4000 and going to 127.0.0.1:4000 in my browser did not work, but leaving the Docker port set as 4000:4000 and running php artisan serve --host=0.0.0.0 did. Now I can visit localhost:4000, 127.0.0.1:4000, or 0.0.0.0:4000 in my browser, and all of them work. Thank you!Gernhard
Also worked for debugging FastAPI through Docker. What's the difference between localhost and 0.0.0.0 ?Sile

© 2022 - 2024 — McMap. All rights reserved.