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

2 years ago

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

Solved

81 Replies

kevmok
PROOP

2 years ago

f78d75b5-8972-4d61-8517-22bbccefb41e


kevmok
PROOP

2 years ago

Do i need to add each service start command to the root package.json?

1271590188409159700


kevmok
PROOP

2 years ago

@Brody This is what I have:

root package.json

"kangiten:start": "pnpm --filter kangiten start"

2 years ago

yes you do, like a start:backend and start:frontend etc


kevmok
PROOP

2 years ago

but

1271627831109095700


kevmok
PROOP

2 years ago

1271627853263409200


kevmok
PROOP

2 years ago

it does fail build though


kevmok
PROOP

2 years ago

let me run that script for logs


2 years ago

ERR_PNPM_NO_LOCKFILE  Cannot install with "frozen-lockfile" because pnpm-lock.yaml is absent

kevmok
PROOP

2 years ago


2 years ago

beat you


kevmok
PROOP

2 years ago

dang


2 years ago

is pnpm-lock.yaml absent?


kevmok
PROOP

2 years ago

does the service need to have that? it's a golang app


kevmok
PROOP

2 years ago

it's in the root


kevmok
PROOP

2 years ago

1271628306067755000


2 years ago

does the go app need to access code from any other folder?


kevmok
PROOP

2 years ago

yeah, I have a shared package for other golang services


2 years ago

okay, do you have a kangiten:build script?


kevmok
PROOP

2 years ago

oh i need to set a specific one too? whoops


kevmok
PROOP

2 years ago

i just had the turbo build one lol


2 years ago

go apps need to be built


kevmok
PROOP

2 years ago

yeah i just assumed it ran turbo build thats mb


2 years ago

youd want app specfic build scripts


kevmok
PROOP

2 years ago

gotcha


kevmok
PROOP

2 years ago

this package json gonna be long haha


2 years ago

do you have this set too? -


kevmok
PROOP

2 years ago

Yeah


kevmok
PROOP

2 years ago

you taught me not too long ago


2 years ago

you may have added the new build script but you didnt configure the railway service to use it


kevmok
PROOP

2 years ago

1271630291601064200


kevmok
PROOP

2 years ago

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

ive had it before


kevmok
PROOP

2 years ago

actually its a go error
```sh: 1: go: not found

/app/services/kangiten:

 ERRPNPMRECURSIVERUNFIRST_FAIL  kangiten@0.0.0 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```


kevmok
PROOP

2 years ago

i set provider to Go though


2 years ago

looks like it didnt take, try removing and readding node and go


kevmok
PROOP

2 years ago

same error


kevmok
PROOP

2 years ago

this must be the issue - i dont see Go in here

1271645472850972700


2 years ago

your providers list is only go though


kevmok
PROOP

2 years ago

Yeah ive been changing them


2 years ago

you want to set it to node and go


kevmok
PROOP

2 years ago

yep it still fails

1271648242798559200


2 years ago

might be time to move to a nixpacks.toml, i dont think you can pass in providers when you use the v2 builder


kevmok
PROOP

2 years ago

ok ill look into that, i ahvent used a nixpacks toml file before


kevmok
PROOP

2 years ago

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"]

2 years ago

tbh i dont know the syntax for the railway.toml file off the top of my head.

so cant hurt to try


kevmok
PROOP

2 years ago

should this stay this way?

1271661761493209000


kevmok
PROOP

2 years ago

gotcha


2 years ago

yeah its not being used anyway


kevmok
PROOP

2 years ago

yeah it might have to be under a key, but the docs dont specify


2 years ago

under a key?


kevmok
PROOP

2 years ago

like [build]


kevmok
PROOP

2 years ago

sorry idk what its called in toml


kevmok
PROOP

2 years ago

yeah based on on the jsonschema it does, let me revisit


kevmok
PROOP

2 years ago

seems that even then it still doesnt work


2 years ago

syntax might be wrong, or you placed the file in the wrong location (need to be on root)


kevmok
PROOP

2 years ago

1271667584307302400
1271667584542179300


2 years ago

i forgot, the v2 builder does not support nixpack configs in a railway.toml/json


2 years ago

use a nixpacks.toml file


kevmok
PROOP

2 years ago

ohhh


kevmok
PROOP

2 years ago

same issue with nixpacks.toml

1271669394165993500


2 years ago

alright, Dockerfile time


2 years ago

or disable the v2 builder


kevmok
PROOP

2 years ago

do yall have any docs or examples for monorepos? i dont have a lot experience with dockerfiles in monorepo. disabling v2 didnt work


kevmok
PROOP

2 years 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


2 years ago

you switched the runtime back to legacy, that should stay on v2.

you wanted to switch off the new builder


2 years ago

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


kevmok
PROOP

2 years ago

no worries


kevmok
PROOP

2 years ago

you mean the new builder env?


2 years ago

yeah


2 years ago

but that looks like its not going to work, so dockerfile for sure


kevmok
PROOP

2 years 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:264701195573133315>

# 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"]

kevmok
PROOP

2 years ago

actually just hardcoding the id without $service_id worked


2 years ago

unfortunately you need to hardcode the service id


2 years ago

awesome


kevmok
PROOP

2 years ago

holy shit it works


kevmok
PROOP

2 years ago

thanks for all your help @Brody 🙏


2 years ago

no problem, dockerfiles imo are better for most things than doing a dance with nixpacks


teocns
PRO

a year ago

Can't this be fixed? it is very annoying that we have to create separate dockerfiles for each service; it's a huge time waste especially for those who don't know they have to hardcode it.


teocns
PRO

a year ago

I just completely removed cache mounts (at the expense of +2min build times). I honestly think we (users) are not meant to go through such hacky processes. Perhaps many other power users (that like running things efficiently) will share the same


a year ago

Railpack is a great option for you then!


a year ago

!s


Status changed to Solved brody 11 months ago


Loading...