25
loading...
This website collects cookies to deliver better user experience
ping mydomian.com // should resolve to your VPS IP address
//server.js file
const fastify = require("fastify")({ logger: true });
fastify.get("/", async (request, reply) => {
return { message: "Hello world! I'm using fastify" };
});
const start = async () => {
try {
await fastify.listen(3000, "0.0.0.0");
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
FROM node:12-alpine
RUN mkdir home/node-traefik
WORKDIR /home/node-traefik
COPY . .
RUN npm install
EXPOSE 3000
CMD [ "node", "server.js" ]
docker-compose up
command.services:
node-server:
build:
context: ./
dockerfile: Dockerfile
ports:
- "3000:3000"
services:
reverse-proxy:
image: traefik:v2.4
container_name: "traefik"
command:
- "--api.insecure=true"
- "--api.dashboard=true"
- "--api.debug=true"
- "--providers.docker=true"
- "--log.LEVEL=DEBUG"
- "--entryPoints.web.address=:80"
- "--entryPoints.websecure.address=:443"
- "--providers.docker.exposedbydefault=false"
- "--certificatesresolvers.myresolver.acme.httpchallenge=true"
- "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=web"
- "[email protected]"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "443:443"
- "80:80"
- "8080:8080"
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "--providers.docker=true"
tells traefik that docker is our key infrastructure components and thus traefik queries the docker API for the relevant information it needs. --api.insecure
enables the traefik dashboard in insecure mode. For production uses cases, you want to use basic authentication and TLS on the dashboard. - "--providers.docker.exposedbydefault=false"
tells traefik not to expose a service unless being explicitly to do so.
The mounted volume with- "/var/run/docker.sock:/var/run/docker.sock:ro"
allows Traefik to communicate with docker.
The - "--entryPoints.web.address=:80"
and - "--entryPoints.websecure.address=:443"
line declare a network and corresponding port entry points into Traefik.
The "[email protected]
creates a certificate resolver named myresolver
. The certificate resolver is responsible for generating, renewing and disposing certificates.
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
Tells our certificate resolver to save our certificates in acme.json file in the letsencrypt volume.
- "--certificatesresolvers.myresolver.acme.httpchallenge=true"
Tells the certificate resolver to use the HTTP challenge.
node-server:
build:
context: .
dockerfile: Dockerfile
container_name: node-server
labels:
- "traefik.enable=true"
- "traefik.http.routers.node-server.rule=Host(`play.paularah.com`)"
- "traefik.http.routers.node-server.entrypoints=websecure"
- "traefik.http.routers.node-server.tls.certresolver=myresolver"
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
- "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
- "traefik.http.routers.redirs.entrypoints=web"
- "traefik.http.routers.redirs.middlewares=redirect-to-https"
Since we configured Traefik not to expose services except being explictly told to do so, The - "traefik.enable=true"
label now exposes our Node.js server container to Traefik.
- "traefik.http.routers.node-server.rule=Host(play.paularah.com)"
creates a router that routes network request from the domian play.paularah.com
to the Node.js server container.
- "traefik.http.routers.node-server.tls.certresolver=myresolver"
tells the router to use the certificate resolver we created earlier.
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
creates a middleware to force-redirect HTTP network request to HTTPS.
play.paularah.com
, uses SSL and force redirects to HTTPS. docker-compose up
everything is up and running. This also makes our entire setup easily reproducible and allowing us move a project easily from one cloud service provider to another.