12 hours ago
Deployed mongo using the helper from railway/iac:
import { mongo, project } from 'railway/iac';
export default defineRailway(() => {
const database = mongo('mongo', { region: 'europe-west4-drams3a' });
return project('dishly', { resources: [database] });
}It created basic service running mongo image, no root user/pass, no connection string var etc. Only:
- Image and volume: mongo:8, with a 50 GB volume at /data/db, in europe-west4-drams3a.
- Variables: only the RAILWAY_* ones. There's no MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD, MONGO_URL, MONGOHOST, MONGOPORT, MONGOUSER or MONGOPASSWORD.
- Start command: none.
The helper is supposed to create what the template does:
- Generate the root username and password.
- Set the MONGO_INITDB_ROOT_* variables.
- Set the MONGO_URL reference variable, which looks like mongodb://${{MONGOUSER}}:${{MONGOPASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017.
- Pass --bind_ip_all in the start command so private networking works.
- Optionally add a TCP proxy for public access.
Versions:
- railway npm package: 3.16.0
- railway cli: 5.69.0
8 Replies
Status changed to Awaiting Railway Response Railway • about 12 hours ago
12 hours ago
This thread has been opened as a bounty so the community can help solve it.
Status changed to Open Railway • about 12 hours ago
11 hours ago
AFAIK, only postgres gets the template setup currently (generated credentials, connection URL variables, TCP proxy etc.). Mongo gets just the image and a volume as you encountered. You need to set up mongodb service manually in this case; I have set up this service to behave exactly as a Railway mongodb template that you can use (adjust values as needed):
import { defineRailway, image, preserve, project, service, volume } from "railway/iac";
const REGION = "europe-west4-drams3a";
export default defineRailway(() => {
const mongo = service("mongo", {
source: image("ghcr.io/railwayapp-templates/mongo-ha/mongo:8.0"),
start:
"docker-entrypoint.sh mongod --ipv6 --bind_ip ::,0.0.0.0 --setParameter diagnosticDataCollectionEnabled=false",
replicas: { [REGION]: 1 },
volumeMounts: { "/data/db": volume("mongo-data", { region: REGION, sizeMB: 5000 }) },
env: {
MONGO_INITDB_ROOT_USERNAME: "mongo",
MONGO_INITDB_ROOT_PASSWORD: preserve(),
MONGOUSER: "${{MONGO_INITDB_ROOT_USERNAME}}",
MONGOPASSWORD: "${{MONGO_INITDB_ROOT_PASSWORD}}",
MONGOHOST: "${{RAILWAY_PRIVATE_DOMAIN}}",
MONGOPORT: "27017",
MONGO_URL:
"mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017",
},
});
return project("dishly", { resources: [mongo] });
});
darseen
AFAIK, only postgres gets the template setup currently (generated credentials, connection URL variables, TCP proxy etc.). Mongo gets just the image and a volume as you encountered. You need to set up mongodb service manually in this case; I have set up this service to behave exactly as a Railway mongodb template that you can use (adjust values as needed): ```ts import { defineRailway, image, preserve, project, service, volume } from "railway/iac"; const REGION = "europe-west4-drams3a"; export default defineRailway(() => { const mongo = service("mongo", { source: image("ghcr.io/railwayapp-templates/mongo-ha/mongo:8.0"), start: "docker-entrypoint.sh mongod --ipv6 --bind_ip ::,0.0.0.0 --setParameter diagnosticDataCollectionEnabled=false", replicas: { [REGION]: 1 }, volumeMounts: { "/data/db": volume("mongo-data", { region: REGION, sizeMB: 5000 }) }, env: { MONGO_INITDB_ROOT_USERNAME: "mongo", MONGO_INITDB_ROOT_PASSWORD: preserve(), MONGOUSER: "${{MONGO_INITDB_ROOT_USERNAME}}", MONGOPASSWORD: "${{MONGO_INITDB_ROOT_PASSWORD}}", MONGOHOST: "${{RAILWAY_PRIVATE_DOMAIN}}", MONGOPORT: "27017", MONGO_URL: "mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017", }, }); return project("dishly", { resources: [mongo] }); }); ```
11 hours ago
I suspected this would be the case. Honestly railway is great, but their iac offering is poor at best and needs some serious work. That’s the single most frustrating thing when working with railway for me atm. I am on pro plan and if there’s one thing that’d make me look for alternative cloud hosting is the iac. Why even add the helper to the library if it ain’t working?!
The above solution won’t work for me, I did it the same way before trying to use helper. However to use Mongo HA one needs to deploy via template.
I guess it’s a manual deploy for me then. That sucks.
11 hours ago
Oh wait! I realised you’re using the template image
image("ghcr.io/railwayapp-templates/mongo-ha/mongo:8.0"),
Would using that image allow me to setup HA? (Asking as I am afk so can’t test atm)
codan84
Oh wait! I realised you’re using the template image image("ghcr.io/railwayapp-templates/mongo-ha/mongo:8.0"), Would using that image allow me to setup HA? (Asking as I am afk so can’t test atm)
11 hours ago
I'll test it, and report back.
darseen
I'll test it, and report back.
11 hours ago
Dw if you’re unsure, I can check it out in like 30min…
11 hours ago
I was able to to set up HA but not by using that image. The mongo-ha image only runs as a replica set when the RS_* variables are set and there are two more data nodes plus an HAProxy edge. I'll paste the IaC file I used to get this working:
import { defineRailway, group, image, preserve, project, service, volume } from "railway/iac";
const REGION = "europe-west4-drams3a";
const ROOT = "mongo";
const NODES = [ROOT, "mongo-2", "mongo-3"];
const ref = (svc: string, key: string) => "${{" + svc + "." + key + "}}";
const MEMBERS = NODES.map((n) => ref(n, "RAILWAY_PRIVATE_DOMAIN") + ":" + ref(ROOT, "MONGO_PORT")).join(",");
const USER = ref(ROOT, "MONGO_INITDB_ROOT_USERNAME");
const PASSWORD = ref(ROOT, "MONGO_INITDB_ROOT_PASSWORD");
function dataNode(name: string) {
const isRoot = name === ROOT;
return service(name, {
source: image("ghcr.io/railwayapp-templates/mongo-ha/mongo:8.0"),
replicas: { [REGION]: 1 },
deploy: { restartPolicyType: "ALWAYS" },
volumeMounts: { "/data/db": volume(`${name}-data`, { region: REGION, sizeMB: 5000 }) },
env: {
MONGO_INITDB_ROOT_USERNAME: isRoot ? "mongo" : USER,
MONGO_INITDB_ROOT_PASSWORD: isRoot ? preserve() : PASSWORD,
MONGO_PORT: isRoot ? "27017" : ref(ROOT, "MONGO_PORT"),
HEALTH_PORT: isRoot ? "8080" : ref(ROOT, "HEALTH_PORT"),
MONGOHOST: "${{RAILWAY_PRIVATE_DOMAIN}}",
MONGOPORT: "${{MONGO_PORT}}",
MONGOUSER: USER,
MONGOPASSWORD: PASSWORD,
MONGO_URL: `mongodb://${USER}:${PASSWORD}@\${{RAILWAY_PRIVATE_DOMAIN}}:\${{MONGO_PORT}}/?directConnection=true`,
RAILWAY_DEPLOYMENT_DRAINING_SECONDS: isRoot ? "60" : ref(ROOT, "RAILWAY_DEPLOYMENT_DRAINING_SECONDS"),
RS_ENABLED: isRoot ? "true" : ref(ROOT, "RS_ENABLED"),
RS_KEY: isRoot ? "${{MONGO_INITDB_ROOT_PASSWORD}}" : ref(ROOT, "RS_KEY"),
RS_NAME: isRoot ? "rs0" : ref(ROOT, "RS_NAME"),
RS_SEEDS: MEMBERS,
},
});
}
export default defineRailway(() => {
const nodes = NODES.map(dataNode);
const edge = service("mongo-ha", {
source: image("ghcr.io/railwayapp-templates/mongo-ha/haproxy:3.2"),
replicas: { [REGION]: 2 },
deploy: { restartPolicyType: "ALWAYS" },
env: {
HAPROXY_CHECK_DOWNINTER: "500ms",
HAPROXY_CHECK_FASTINTER: "500ms",
HAPROXY_CHECK_INTERVAL: "3s",
HAPROXY_MAX_CONN: "10000",
HAPROXY_TIMEOUT_CHECK: "3s",
HAPROXY_TIMEOUT_CLIENT: "1d",
HAPROXY_TIMEOUT_CONNECT: "10s",
HAPROXY_TIMEOUT_SERVER: "1d",
HEALTH_CHECK_PORT: ref(ROOT, "HEALTH_PORT"),
MONGO_NODES: MEMBERS,
MONGO_PORT: "27017",
MONGOHOST: "${{RAILWAY_PRIVATE_DOMAIN}}",
MONGOPORT: "${{MONGO_PORT}}",
MONGOUSER: USER,
MONGOPASSWORD: PASSWORD,
MONGO_URL: `mongodb://${USER}:${PASSWORD}@\${{RAILWAY_PRIVATE_DOMAIN}}:\${{MONGO_PORT}}/?directConnection=true`,
},
});
return project("dishly", { resources: [...group("MongoDB", [...nodes, edge])] });
});
7 hours ago
I will test this out when I get a chance, but honestly, if Railway dev team reads these topics - maybe just remove the mongo helper from iac if it ain't working? Instead of making people go these rabbbit holes... or better yet, invest more resources in getting proper working IaC (Pulumi provider even maybe)... It's a 1-2 weeks worth of work, I don't believe you can't do that.
an hour ago
https://docs.railway.com/infrastructure-as-code/reference#databases
@railway - i am sure this can be classed as a bug. Please update your IaC docs to remove the non-working DBs, or probably better - fix this.