|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Understanding the Port parameter |
| 4 | + description: Learn about the Port parameter and the PORT environment variable in Scaleway Serverless Containers. |
| 5 | +content: |
| 6 | + h1: Understanding the Port parameter |
| 7 | + paragraph: Learn about the Port parameter and the PORT environment variable in Scaleway Serverless Containers. |
| 8 | +tags: port variable environment containers serverless |
| 9 | +dates: |
| 10 | + validation: 2025-04-02 |
| 11 | + posted: 2025-04-02 |
| 12 | +categories: |
| 13 | + - serverless |
| 14 | + - containers |
| 15 | + - environment-variables |
| 16 | +--- |
| 17 | + |
| 18 | +## Port parameter |
| 19 | + |
| 20 | +The port of a containerized application refers to the network port that the application inside the container listens on for incoming requests. |
| 21 | + |
| 22 | +When you deploy a container, you must map this internal port to a port on the host machine by specifying its value at [container creation](/serverless-containers/how-to/deploy-container/) via the **Port** parameter. The value defined in this parameter will then be passed to your container during the deployment inside the `PORT` environment variable. |
| 23 | + |
| 24 | +<Message type="note"> |
| 25 | +- Only one port can be exposed per Serverless Container. |
| 26 | +- Your container is accessible from the internet via ports 80 and 443, regardless of the specified port. The value you set determines how the Scaleway infrastructure accesses your container. |
| 27 | +</Message> |
| 28 | + |
| 29 | +## PORT environment variable |
| 30 | + |
| 31 | +To allow you application to be reachable, the port declared as a parameter when [creating your Container](/serverless-containers/how-to/deploy-container/) must be the same as the port exposed by your containerized application. |
| 32 | + |
| 33 | +We therefore recommend you use the `$PORT` variable in your application, as it will contain the port parameter value, as shown in the examples below. |
| 34 | + |
| 35 | +### nginx example |
| 36 | + |
| 37 | +```dockerfile |
| 38 | +FROM nginx:alpine |
| 39 | + |
| 40 | +# Create a minimal nginx config that will be modified at runtime |
| 41 | +RUN echo 'worker_processes 1; \ |
| 42 | + events { worker_connections 1024; } \ |
| 43 | + http { \ |
| 44 | + server { \ |
| 45 | + listen REPLACE_PORT default_server; \ |
| 46 | + location / { return 200 "Hello from Nginx on Scaleway Serverless Containers!\n"; } \ |
| 47 | + } \ |
| 48 | + }' > /etc/nginx/nginx.conf |
| 49 | + |
| 50 | +# Simple startup script that replaces the port |
| 51 | +CMD ["/bin/sh", "-c", "sed -i s/REPLACE_PORT/$PORT/g /etc/nginx/nginx.conf && exec nginx -g 'daemon off;'"] |
| 52 | +``` |
| 53 | + |
| 54 | +### NodeJS example |
| 55 | + |
| 56 | +```dockerfile |
| 57 | +# Use the official Node.js slim image |
| 58 | +FROM node:22-slim |
| 59 | + |
| 60 | +# Create app directory |
| 61 | +WORKDIR /usr/src/app |
| 62 | + |
| 63 | +# Create package.json and simple Express app directly in Dockerfile |
| 64 | +RUN echo '{"name":"scaleway-serverless","version":"1.0.0","description":"","main":"server.js","scripts":{"start":"node server.js"},"dependencies":{"express":"^5"}}' > package.json && \ |
| 65 | + npm install && \ |
| 66 | + echo "const express = require('express');\nconst app = express();\nconst port = process.env.PORT || 8080;\n\napp.get('/', (req, res) => {\n res.send('<!DOCTYPE html><html><body><h1>Hello from Scaleway Serverless!</h1></body></html>');\n});\n\napp.listen(port, () => {\n console.log(`Server running on port \${port}`);\n});" > server.js |
| 67 | + |
| 68 | +# Start the application |
| 69 | +CMD ["npm", "start"] |
| 70 | +``` |
| 71 | + |
| 72 | +### Python Flask example |
| 73 | + |
| 74 | +```Dockerfile |
| 75 | +# Use the official Python slim image |
| 76 | +FROM python:3.13-slim |
| 77 | + |
| 78 | +# Install Flask |
| 79 | +RUN pip install flask gunicorn |
| 80 | + |
| 81 | +# Create a simple Flask app directly in the Dockerfile |
| 82 | +RUN echo "from flask import Flask\napp = Flask(__name__)\n\n@app.route('/')\ndef hello():\n return '<!DOCTYPE html><html><body><h1>Hello from Flask on Scaleway Serverless!</h1></body></html>'\n\nif __name__ == '__main__':\n app.run(host='0.0.0.0', port=8080)" > app.py |
| 83 | + |
| 84 | +# Run the app with Gunicorn |
| 85 | +CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app |
| 86 | +``` |
0 commit comments