Skip to content

Instantly share code, notes, and snippets.

@yooouuri
Created November 23, 2024 22:01
Show Gist options
  • Select an option

  • Save yooouuri/043746d24b82d1b9392d0e77c98f39ab to your computer and use it in GitHub Desktop.

Select an option

Save yooouuri/043746d24b82d1b9392d0e77c98f39ab to your computer and use it in GitHub Desktop.

Make sure to point an A record into your VPS IP.

docker network create --driver bridge local

Create /nginx/conf.d/app.conf

server {
    listen 80;
    listen [::]:80;

    server_name domain.tld www.domain.tld;

    location / {
        proxy_set_header Host $host;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://app:3000/;
    }
}

Then run the container.

docker run -d \
  --network local \
  -p 80:80 \
  --restart always \
  -v $(pwd)/nginx/conf.d/:/etc/nginx/conf.d/:ro \
  nginx:latest

The nginx container routes the request (proxy) to the app container. Thats it.

Create a index.js

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Make sure to add express to your dependencies in package.json

FROM node
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["node","index.js"]

Build and start a container with --network local and --name app. Hence the app name, its important.

The app container is listening on app:3000, the NGINX container routes requests to domain.tld and www.domain.tld to app:3000 in the docker network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment