GenieFramework / Genie.jl

🧞The highly productive Julia web framework
https://genieframework.com
MIT License
2.26k stars 190 forks source link

App can not be accessed in docker container #331

Closed essenciary closed 3 years ago

essenciary commented 3 years ago

Per feedback in Gitter

NLaws commented 3 years ago

is this fixable by adding an ingress layer (such as Nginx)? I have been trying to use the basic json example in a container but have not been able to access the Genie port from the host.

From the docker-compose.yml:

...

  julia:
    container_name: julia_api
    build:
      context: .
    command: julia --project="/opt/julia_src" api.jl
    ports:
      - "8081:8081"

and api.jl:

using Genie, Genie.Router, Genie.Requests, Genie.Renderer.Json
include("REopt.jl")
using .REopt

Genie.config.run_as_server = true

route("/jsonpayload", method = POST) do
  data = jsonpayload() # Dict{String,Any}
  results = reopt(data)
  json(results)
end

Genie.startup(8081)
ndgnuh commented 3 years ago

I'm having the same problem. I don't know why but it will works when your host is 0.0.0.0, but not 127.0.0.1. Try

Genie.startup(8081, "0.0.0.0") # Genie use 127.0.0.1 by default
NLaws commented 3 years ago

@ndgnuh I think that is the correct solution and it has to do with Docker networks (though it didn't work for me so I went with HTTP.jl instead - but it might not have worked for me because of unrelated problems).

ndgnuh commented 3 years ago

Ah, I didn't read the docs thoroughly. You can change the environment using the GENIE_ENV variable, the host and port will be configured by the config/env/$GENIE_ENV.jl file. Change the host to 0.0.0.0 in the dev environment (default) or use the prod environment and everything will work.

guimarqu commented 3 years ago

Hi,

We deployed a Genie web API in a kubernetes cluster. After configuring the deployment, service and ingress, we could not reach the web API from outside. We had 404 or 502 errors. The problem was that we were running Genie jl using the host 127.0.0.1 instead of 0.0.0.0. So make sure you use 0.0.0.0.

essenciary commented 3 years ago

@guimarqu Thanks for the contribution. But it sounds like maybe you're not running the app in production mode (environment), as that, by default, will use 0.0.0.0

I strongly suggest you run it in production mode as that has a considerable and noticeable performance boost (it's optimized for performance, whereas the dev mode is optimized for building and debugging).

image

To do that you can either pass the environment variable GENIE_ENV=prod or set ENV["GENIE_ENV"]="prod" in config/global.jl.

guimarqu commented 3 years ago

Thanks very much for your advices.

We tried to run it in prod but the host was 127.0.0.1 despite what's written in the prod configuration file. We are going to look more carefully for what we did wrong.

Thanks again !

essenciary commented 3 years ago

You're welcome!

Weird... maybe you're passing the host explicitly? If you can reproduce, please open an issue. Thanks!

Krastanov commented 1 year ago

This still seems to happen. GENIE_ENV=prod but serving from the wrong ip and port:

image

To reproduce you can use https://github.com/QuantumSavory/QuantikzServer.jl

Krastanov commented 1 year ago

as discussed in https://github.com/GenieFramework/Genie.jl/issues/651, my issue was that I was using Genie v5 to run an app created with Genie v1

essenciary commented 1 year ago

@Krastanov ok, so you need to update the configuration file? Works well now?

Krastanov commented 1 year ago

yes, regenerating the app and copying over routes.jl fixed the issue