Home Developing Nodejs Webservers with Dockerized code-server Environment using Traefik
Post
Cancel

Developing Nodejs Webservers with Dockerized code-server Environment using Traefik

After finally getting a chance to reflefct on my server management when I realized I still haven’t solved a problem that I gave up on solving for a while when I couln’t find proper documentation on using traefik labels to route a code-server container along with future nodejs servers created inside of the code-server container’s terminal. In the end, there wasn’t much that I was missing from the docs, I just needed to know how to put everything together.

Traefik Labels

Fixing my setup with traefik turned out to be the easy part. When adding the labels to the docker container, I missed the service definitions which caused improper routing. Also, I needed to set the environment variable PROXY_DOMAIN=example.com which allows for ports to be oppened at [port].example.com instead of example.com/proxy/[port]. This makes it easy to setup each service with a unique CNAME

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    labels:
#code-server
      traefik.enable: true
      traefik.http.routers.vscode.entryPoints: https
      traefik.http.routers.vscode.rule: Host(`codeserver.example.com`)
      traefik.http.routers.vscode.middlewares: auth@file #authelia only
      traefik.http.routers.vscode.service: vscode
      traefik.http.services.vscode.loadbalancer.server.port: 8443

#Nodejs Webserver 
      traefik.http.routers.portfolio.entryPoints: https
      traefik.http.routers.portfolio.rule: Host(`5173.example.com`)
      traefik.http.routers.portfolio.middlewares: auth@file #authelia only
      traefik.http.services.portfolio.loadbalancer.server.port: 5173
      traefik.http.routers.portfolio.service: portfolio
    environment:
      - PROXY_DOMAIN=example.com 

In order for the container to start without traefik causing problems, a change needs to be made to the traefik.yml file. The docker provider needs to contain allowEmptyServices: true so the future web servers can be included as services

1
2
3
4
5
6
7
8
9
10
  docker:
    useBindPortIP: true
    allowEmptyServices: true
    watch: true
    network: marbles # Add Your Docker Network Name Here
    # Default host rule to containername.domain.example
    defaultRule: "Host(`.marsblars.dev`)"
    swarmModeRefreshSeconds: 15s
    exposedByDefault: false
    endpoint: "tcp://dockersocket:2375" # Uncomment if you are using docker socket proxy

code-server nodejs development

Code-server had some issues that needed to be fixed in order to work with traefik and node. First a dockerfile needs to be created for node installation to be persistent. This dockerfile only covers node installation but it should also include any other languages that you want to develop with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM lscr.io/linuxserver/code-server:latest 

USER 1000[:1000] 

#dependencies
RUN apt-get update && apt-get install -y curl ca-certificates gnupg wget git 



#Node Version Manager Install Script
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash  

#nodejs install
RUN apt-get update && nvm install node 
#Fixes System limit for number of file watchers reached error
RUN echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf && sysctl -p

Next the container can be redefined in the docker-compose file to include the build instructions for the dockerfile in the same directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  vscode:
    container_name: vscode
    networks:
      - marbles
    labels:
#code-server
      traefik.enable: true
      traefik.http.routers.vscode.entryPoints: https
      traefik.http.routers.vscode.rule: Host(`codeserver.example.com`)
      traefik.http.routers.vscode.middlewares: auth@file #authelia only
      traefik.http.routers.vscode.service: vscode
      traefik.http.services.vscode.loadbalancer.server.port: 8443

#Nodejs Webserver 
      traefik.http.routers.portfolio.entryPoints: https
      traefik.http.routers.portfolio.rule: Host(`5173.example.com`)
      traefik.http.routers.portfolio.middlewares: auth@file #authelia only
      traefik.http.services.portfolio.loadbalancer.server.port: 5173 #port created when running npm run dev
      traefik.http.routers.portfolio.service: portfolio
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Los_Angeles
      - PROXY_DOMAIN=example.com 
    volumes:
      - /home/tv/vscode:/config
      - /home/tv:/main
    restart: unless-stopped
    build:
      context: . 
      dockerfile: Dockerfile
This post is licensed under CC BY 4.0 by the author.