Network /
Expose your application
You will need to expose the relevant port(s) in your application to make it available on private or public networks if this is not configured by default.
If you're using a Dockerfile you should use the EXPOSE command to specify which ports the container listens on . This does not expose the ports in your application, but informs Northflank which ports your deployment should publish.
You may need to specifically bind your application to all available IP addresses by specifying the hostname as 0.0.0.0
in order to accept incoming connections from the container it is running in, while some applications bind to this hostname by default.
Below is a list of commonly-deployed applications and examples of the necessary commands to run them on the desired port. You should refer to the documentation for the language or framework you are using for more detailed information, and use environment variables rather than hardcoded settings where possible.
Node
To run a Node server , the server must be bound to hostname 0.0.0.0
and given a port to listen on. Specify the port in the Dockerfile and call your main file with the node
command.
App example
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname);
Docker commands example
EXPOSE 3000
CMD ["node", "index.js"]
NGINX
Many web applications use NGINX to serve a generated site, for example Angular . By default, NGINX listens on port 80, but this can be changed in the NGINX configuration file if required.
Docker commands example
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Express
To serve an Express.js application , ensure the Express application is set to listen on your desired port, then expose that port in the Dockerfile and include the command to run the server.
You can find an example of a Node Express Server in this repository .
Application example
const express = require('express');
const app = express();
const port = 80;
app.listen(port);
Docker commands example
EXPOSE 80
CMD [ "yarn", "run", "start" ]
Next
A Next.js application runs by default on hostname 0.0.0.0
and port 3000
. You can change the port by adding an environment variable called PORT
, with the value of the port you want to use. You should update your Dockerfile and the network settings of any deployments to reflect the new port.
You can find an example of a Next.js application in this repository .
Docker commands example
EXPOSE 3000
CMD ["node_modules/.bin/next", "start"]
Flask
To run a Python Flask application you will need to include a WSGI server .
You can find an example of a Python Flask application in this repository . This example uses the Waitress server .
Application example
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=80)
Docker commands example
ENTRYPOINT ["python"]
CMD ["main.py"]
EXPOSE 80
Django
To run a Python Django application you will need to run the server with the command python manage.py runserver
, binding to 0.0.0.0
with the port specified. The example below uses port 80
.
You can find an example of a Python Django application in this repository .
Docker commands example
EXPOSE 80
CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]
PHP Laravel
To serve a Laravel application you must run the PHP artisan command to serve the site, binding to hostname 0.0.0.0
with the option --host=
. You can use the option --port=
to specify the port to listen on.
You can find an example of a Laravel application in this repository .
Docker commands example
CMD [ "php", "artisan", "serve", "--host=0.0.0.0", "--port=80" ]
EXPOSE 80
Ruby on Rails
To serve a Ruby on Rails application , you must expose the port (default 3000
) and run the Rails server, bound to 0.0.0.0
.
You can find an example of a Ruby on Rails application in this repository .
Configuration example
While developing on your local machine, your Rails server will be available on localhost
, however you will need to authorize hosts to make it accessible when deployed.
You can configure your application to accept connections on Northflank by adding the following to the relevant environment configuration file in config/environments
. The NF_HOSTS
environment variable is injected to all runtime containers on Northflank and contains all the configured domain names for the server.
if ENV["NF_HOSTS"].present?
config.hosts = ENV["NF_HOSTS"]
end
Docker commands example
EXPOSE 3000
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
Rust
To serve a Rust application you can use a web application framework, in our example we use nickel to listen on port 6767
, and bound to 0.0.0.0
.
You can find an example of a Rust application in this repository .
Rust application example
#[macro_use] extern crate nickel;
use nickel::{Nickel, StaticFilesHandler};
fn main() {
let mut server = Nickel::new();
server.utilize(StaticFilesHandler::new("static/"));
server.listen("0.0.0.0:6767").unwrap();
}
Docker commands example
EXPOSE 6767
CMD ["./rust-docker-web"]