0

I have a project that when running locally, outputs server started at /127.0.0.1:5000 and I can access it locally on the said port just fine.

I am trying to run it through docker. I have the following:

DockerFile:

FROM mozilla/sbt

ADD build.sbt /root/build/
RUN cd /root/build && sbt compile

EXPOSE 5000
WORKDIR /root/build

CMD sbt run

and the following docker-compose.yml:

version: '3.1'

services:
  sbt:
    build:
      context: ./
      dockerfile: ./Dockerfile
    image: sbt
    ports:
      - "8080:5000"
    volumes:
      - "./:/root/build"

I try running it through docker-compose up and I can see the logs about the server starting, but can't access the service through the specified port, namely 8080. Am I missing something?

fyi, the above setup is inspired by this post where I have changed the base image and also got rid of the external-network bit that I did not understand.

Maths noob
  • 1,684
  • 20
  • 42
  • Adding the build.sbt may help someone reproduce this. Can you access it when you build and run using just the Dockerfile? – Brian Dec 02 '21 at 18:49
  • trying to figure out how to mount a volume when running straight from the dockerfile. it doesn't like it: `docker run -it -v "./:/root/build" $(docker build -q .) ` – Maths noob Dec 02 '21 at 19:12
  • 1
    Anything that outputs "server started at 127.0.0.1:5000" probably won't be reachable from outside Docker. You don't say anything about what the process in the container is (presumably something Scala-based) but setting it to listen on 0.0.0.0 might make inter-container communication and the `ports:` setting work. See also for example [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip). – David Maze Dec 02 '21 at 23:24
  • bravo @DavidMaze that did it :) do you want to post it as an answer, or should I post an answer myself? – Maths noob Dec 03 '21 at 00:38

1 Answers1

0

If app starts by default on port 5000, bu you need to start it in another port with docker you should use:

ports:
      - "8080:5000"

Internally your app continues using the 5000 port but docker bind that port to another, in the example : 8080

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • ah good catch. I always used the same port so got the order wrong. I made the change you suggested, stopped the server, ran `docker compose up` again and tried accessing `http://localhost:8080/foo/bar` but no joy. (updated the question body) – Maths noob Dec 02 '21 at 19:30
  • 1
    #1 is the app running? #2 do you have a error log? #3 enter to the container `docker exec -it myapp bash` and then execute `curl localhost:5000/foo/bar` if not works,, the problem is with your app , not with docker – JRichardsz Dec 02 '21 at 19:36
  • yeah I can successfully hit that endpoint from inside the container. :/ – Maths noob Dec 02 '21 at 19:46
  • are you on windows? attach the curl localhost:8080/foo/bar -v log or compare it with the internal curl – JRichardsz Dec 02 '21 at 19:48
  • on mac. im getting: ``` curl http://localhost:8080/foo/bar -v * Trying ::1... * TCP_NODELAY set * Connected to localhost (::1) port 8080 (#0) > GET /foo/bar HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.54.0 > Accept: */* > * Empty reply from server * Connection #0 to host localhost left intact curl: (52) Empty reply from server ``` internal curl gives me the json response. – Maths noob Dec 02 '21 at 19:52
  • I even tried `docker-compose up --force-recreate` to make sure there is no weird caching going on. still the same issue. – Maths noob Dec 02 '21 at 20:06