R
Railway4mo ago
Boxer

What's the best way to start services in a monorepo with a lot of services?

Looking at the docs I see commands like npm run start:backend so how does railway now which service is which? https://docs.railway.app/guides/monorepo#deploying-a-shared-monorepo I have a turborepo with golang services in the following directory structure /root --/services -- (golang services 1 folder each) --/packages --/apps So now I am not sure how to start each monorepo service in each railway service
Railway Docs
Deploying a Monorepo | Railway Docs
Documentation for Railway
Solution:
alright, Dockerfile time
Jump to solution
59 Replies
Percy
Percy4mo ago
Project ID: f78d75b5-8972-4d61-8517-22bbccefb41e
Boxer
BoxerOP4mo ago
f78d75b5-8972-4d61-8517-22bbccefb41e
Boxer
BoxerOP4mo ago
Do i need to add each service start command to the root package.json?
No description
Boxer
BoxerOP4mo ago
@Brody This is what I have: root package.json
"kangiten:start": "pnpm --filter kangiten start"
"kangiten:start": "pnpm --filter kangiten start"
Brody
Brody4mo ago
yes you do, like a start:backend and start:frontend etc
Boxer
BoxerOP4mo ago
No description
Boxer
BoxerOP4mo ago
it does fail build though let me run that script for logs
Brody
Brody4mo ago
ERR_PNPM_NO_LOCKFILE  Cannot install with "frozen-lockfile" because pnpm-lock.yaml is absent
ERR_PNPM_NO_LOCKFILE  Cannot install with "frozen-lockfile" because pnpm-lock.yaml is absent
Brody
Brody4mo ago
beat you
Boxer
BoxerOP4mo ago
dang
Brody
Brody4mo ago
is pnpm-lock.yaml absent?
Boxer
BoxerOP4mo ago
does the service need to have that? it's a golang app it's in the root
Boxer
BoxerOP4mo ago
No description
Brody
Brody4mo ago
does the go app need to access code from any other folder?
Boxer
BoxerOP4mo ago
yeah, I have a shared package for other golang services
Brody
Brody4mo ago
okay, do you have a kangiten:build script?
Boxer
BoxerOP4mo ago
oh i need to set a specific one too? whoops i just had the turbo build one lol
Brody
Brody4mo ago
go apps need to be built
Boxer
BoxerOP4mo ago
yeah i just assumed it ran turbo build thats mb
Brody
Brody4mo ago
youd want app specfic build scripts
Boxer
BoxerOP4mo ago
gotcha this package json gonna be long haha
Brody
Brody4mo ago
Boxer
BoxerOP4mo ago
Yeah you taught me not too long ago
Brody
Brody4mo ago
you may have added the new build script but you didnt configure the railway service to use it
Boxer
BoxerOP4mo ago
No description
Boxer
BoxerOP4mo ago
ahh this error WARN  Local package.json exists, but node_modules missing, did you mean to install? ive had it before actually its a go error
sh: 1: go: not found

/app/services/kangiten:

 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  [email protected] build: `go build -o bin/kangiten main.go`

spawn ENOENT

 WARN  Local package.json exists, but node_modules missing, did you mean to install?

 ELIFECYCLE  Command failed with exit code 1.

error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1

exit status 1
sh: 1: go: not found

/app/services/kangiten:

 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  [email protected] build: `go build -o bin/kangiten main.go`

spawn ENOENT

 WARN  Local package.json exists, but node_modules missing, did you mean to install?

 ELIFECYCLE  Command failed with exit code 1.

error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1

exit status 1
i set provider to Go though
Brody
Brody4mo ago
looks like it didnt take, try removing and readding node and go
Boxer
BoxerOP4mo ago
same error
Boxer
BoxerOP4mo ago
this must be the issue - i dont see Go in here
No description
Brody
Brody4mo ago
your providers list is only go though
Boxer
BoxerOP4mo ago
Yeah ive been changing them
Brody
Brody4mo ago
you want to set it to node and go
Boxer
BoxerOP4mo ago
yep it still fails
No description
Brody
Brody4mo ago
might be time to move to a nixpacks.toml, i dont think you can pass in providers when you use the v2 builder https://nixpacks.com/docs/guides/configuring-builds#change-what-providers-are-run
Boxer
BoxerOP4mo ago
ok ill look into that, i ahvent used a nixpacks toml file before Is this the correct way to do it. I added a railway.toml to the root of my monorepo with the following:
[build]
builder = "NIXPACKS"

providers = ["node", "go"]
[build]
builder = "NIXPACKS"

providers = ["node", "go"]
Brody
Brody4mo ago
tbh i dont know the syntax for the railway.toml file off the top of my head. so cant hurt to try
Boxer
BoxerOP4mo ago
should this stay this way?
No description
Boxer
BoxerOP4mo ago
gotcha
Brody
Brody4mo ago
yeah its not being used anyway
Boxer
BoxerOP4mo ago
yeah it might have to be under a key, but the docs dont specify
Brody
Brody4mo ago
under a key?
Boxer
BoxerOP4mo ago
like [build] sorry idk what its called in toml yeah based on on the jsonschema it does, let me revisit seems that even then it still doesnt work
Brody
Brody4mo ago
syntax might be wrong, or you placed the file in the wrong location (need to be on root)
Boxer
BoxerOP4mo ago
No description
No description
Brody
Brody4mo ago
i forgot, the v2 builder does not support nixpack configs in a railway.toml/json use a nixpacks.toml file
Boxer
BoxerOP4mo ago
ohhh
Boxer
BoxerOP4mo ago
same issue with nixpacks.toml
No description
Solution
Brody
Brody4mo ago
alright, Dockerfile time
Brody
Brody4mo ago
or disable the v2 builder
Boxer
BoxerOP4mo ago
do yall have any docs or examples for monorepos? i dont have a lot experience with dockerfiles in monorepo. disabling v2 didnt work
Boxer
BoxerOP4mo ago
from what im seeing i guess i could make a dockerfile for each service with custom docker file path https://docs.railway.app/guides/dockerfiles
Railway Docs
Build from a Dockerfile | Railway Docs
Documentation for Railway
Brody
Brody4mo ago
you switched the runtime back to legacy, that should stay on v2. you wanted to switch off the new builder but docs on how to write a Dockerfile would not be specific to railway so it would be out of the scope of our docs
Boxer
BoxerOP4mo ago
no worries you mean the new builder env?
Brody
Brody4mo ago
yeah but that looks like its not going to work, so dockerfile for sure
Boxer
BoxerOP4mo ago
@Brody I see some people having the Cache mount ID is not prefixed with cache key which I am myself having but don't see the issue :Thonk:
# Build stage
FROM golang:1.22.5 AS builder

# Set working directory
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Set the service name (change this for each service)
ARG SERVICE_NAME=kangiten
ARG SERVICE_ID=fc824b79-e4db-4095-9801-b99985b8f4c8

# Download dependencies
RUN --mount=type=cache,id=s/${SERVICE_ID}-/root/cache/go-build,target=/root/.cache/go-build \
go mod download

# Copy source code
COPY . .

# Build the application
RUN --mount=type=cache,id=s/${SERVICE_ID}-/root/cache/go-build,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -o /app/main ./services/${SERVICE_NAME}

# Final stage
FROM alpine:3.18

# Set working directory
WORKDIR /app

# Copy the binary from the builder stage
COPY --from=builder /app/main .

# Run the binary
CMD ["./main"]
# Build stage
FROM golang:1.22.5 AS builder

# Set working directory
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Set the service name (change this for each service)
ARG SERVICE_NAME=kangiten
ARG SERVICE_ID=fc824b79-e4db-4095-9801-b99985b8f4c8

# Download dependencies
RUN --mount=type=cache,id=s/${SERVICE_ID}-/root/cache/go-build,target=/root/.cache/go-build \
go mod download

# Copy source code
COPY . .

# Build the application
RUN --mount=type=cache,id=s/${SERVICE_ID}-/root/cache/go-build,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -o /app/main ./services/${SERVICE_NAME}

# Final stage
FROM alpine:3.18

# Set working directory
WORKDIR /app

# Copy the binary from the builder stage
COPY --from=builder /app/main .

# Run the binary
CMD ["./main"]
actually just hardcoding the id without $service_id worked
Brody
Brody4mo ago
unfortunately you need to hardcode the service id awesome
Boxer
BoxerOP4mo ago
holy shit it works thanks for all your help @Brody 🙏
Brody
Brody4mo ago
no problem, dockerfiles imo are better for most things than doing a dance with nixpacks
Want results from more Discord servers?
Add your server