Recently I was playing around with building an express.js app and running it in Docker. I used the standard npm init
to create my app, wrote my code, tested it, and was happy.
Then I tried to deploy it in Marathon. No matter what I did, I couldn’t get it to load. It was as if the port forwarding just didn’t work. I even docker-enter‘ed into the container, and couldn’t connect to Node – but it was running.
As a last resort, I went poking around the auto-generated code that express creates, and I saw this line:
/** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '3000'); app.set('port', port);
and the light bulb finally went off.
Marathon sets the PORT
environment variable when launching a Docker container so that the app knows which port it’s proxying to (helpful for service registration, etc). In this case, Express was using that environment variable and listening on that port instead of the expected 3000. I just removed that check and we’re good to go!