Crashes after I setup Tailscale for NextJS app?

Hi,

I’m trying to setup a Tailscale for a NextJS app. This is my setup:

Dockerfile:

# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=lts
FROM node:${NODE_VERSION}-alpine as base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apk update && \
    apk add build-base gyp pkgconfig python3

# Install node modules
COPY package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --omit=dev


# Final stage for app image
FROM base

# Install sharp for next/Image
RUN npm install sharp

# https://tailscale.com/kb/1132/flydotio/ (simplified)
RUN apk update && apk add ca-certificates iptables ip6tables && rm -rf /var/cache/apk/*
COPY --from=docker.io/tailscale/tailscale:stable /usr/local/bin/tailscaled /app/tailscaled
COPY --from=docker.io/tailscale/tailscale:stable /usr/local/bin/tailscale /app/tailscale
RUN mkdir -p /var/run/tailscale /var/cache/tailscale /var/lib/tailscale

# Copy built application
COPY --from=build /app/.next/standalone /app
COPY --from=build /app/.next/static /app/.next/static
COPY --from=build /app/public /app/public
COPY --from=build /app/start.sh /app/start.sh

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
# CMD [ "node", "server.js" ]
CMD ["/app/start.sh"]

start.sh:

#!/bin/sh

/app/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock &
/app/tailscale up --auth-key=${TAILSCALE_AUTHKEY} --hostname=fly-app
node server.js

However, the app crashes with

	
	
2024-11-06 18:18:20.907	
 WARN could not unmount /rootfs: EINVAL: Invalid argument
	
	
2024-11-06 18:18:20.900	
 INFO Starting clean up.
	
	
2024-11-06 18:18:20.886	
 INFO Main child exited normally with code: 1
	
	
2024-11-06 18:18:20.130	
Node.js v22.11.0
	
	
2024-11-06 18:18:20.130	
at node:internal/main/run_main_module:36:49
	
	
2024-11-06 18:18:20.130	
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
	
	
2024-11-06 18:18:20.130	
at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
	
	
2024-11-06 18:18:20.130	
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
	
	
2024-11-06 18:18:20.130	
at Function._load (node:internal/modules/cjs/loader:1128:12)
	
	
2024-11-06 18:18:20.130	
at Module.load (node:internal/modules/cjs/loader:1318:32)
	
	
2024-11-06 18:18:20.130	
at Object..js (node:internal/modules/cjs/loader:1689:10)
	
	
2024-11-06 18:18:20.130	
at Module._compile (node:internal/modules/cjs/loader:1491:20)
	
	
2024-11-06 18:18:20.130	
at wrapSafe (node:internal/modules/cjs/loader:1469:18)
	
	
2024-11-06 18:18:20.130	
SyntaxError: Invalid regular expression flags
	
	
2024-11-06 18:18:20.130	
^
	
	
2024-11-06 18:18:20.130	
/app/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock &
	
	
2024-11-06 18:18:20.130	
/app/start.sh:3
	
	
2024-11-06 18:18:20.019	
2024/11/06 23:18:20 INFO SSH listening listen_address=[fdaa:0:c47a:a7b:e824:dcb7:e37b:2]:22 dns_server=[fdaa::3]:53
	
	
2024-11-06 18:18:19.933	
Machine started in 876ms

What am I doing wrong?

What is on this line?

That’s the important error here.

What version of node are you using on your development machine? What version is specified in the Dockerfile?

I was using 21.7.2 on my dev and lts-alpine in the Dockerfile, which corresponds to 22.11.0 according to Docker.

I updated the Dockerfile to 21.7.2-alpine but I’m getting the same error:

	
	
2024-11-06 21:47:17.153	
Node.js v21.7.2
	
	
2024-11-06 21:47:17.153	
at node:internal/main/run_main_module:28:49
	
	
2024-11-06 21:47:17.153	
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
	
	
2024-11-06 21:47:17.153	
at Module._load (node:internal/modules/cjs/loader:1021:12)
	
	
2024-11-06 21:47:17.153	
at Module.load (node:internal/modules/cjs/loader:1205:32)
	
	
2024-11-06 21:47:17.153	
at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
	
	
2024-11-06 21:47:17.153	
at Module._compile (node:internal/modules/cjs/loader:1331:27)
	
	
2024-11-06 21:47:17.153	
at wrapSafe (node:internal/modules/cjs/loader:1279:20)
	
	
2024-11-06 21:47:17.153	
at internalCompileFunction (node:internal/vm:128:18)
	
	
2024-11-06 21:47:17.153	
SyntaxError: Invalid regular expression flags
	
	
2024-11-06 21:47:17.153	
^
	
	
2024-11-06 21:47:17.153	
/app/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock &
	
	
2024-11-06 21:47:17.153	
/app/start.sh:3

I don’t know what’s in run_main_module. I think it’s nodejs internal code which I’m not very knowledgeable about.

I figured it out.

The problem was that the default ENTRYPOINT is docker-entrypoint.sh, which basically is just node $@, so in the above attempt, I’m guessing it was running node /app/start.sh.

The fix is to run ENTRYPOINT [“/app/start.sh”]