Find code examples for this guide on GitHub.
Initial setup
Create a Hello World app and Dockerfile, then deploy to Railway.
# Dockerfile
FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
# .railwayignore
.env.keys
!.env.production
npx @railway/cli@latest init
npx @railway/cli@latest up
npx @railway/cli@latest domain
Set PORT to 3000 (or your app's listen port) in the Railway dashboard, then redeploy.
Run dotenvx
Install dotenvx in your Dockerfile and prepend your app command with dotenvx run --.
# Dockerfile
FROM node:20
WORKDIR /app
RUN curl -sfS https://dotenvx.sh/install.sh | sh
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["dotenvx", "run", "--", "node", "index.js"]
Encrypt production
# .env.production
HELLO="production"
$ dotenvx set HELLO production -f .env.production
Commit .env.production. Do not commit .env.keys.
Set decryption key
Set DOTENV_PRIVATE_KEY_PRODUCTION in the Railway environment variable manager (apply the change), then redeploy.
npx @railway/cli@latest up
Your app reboots and env is injected from the encrypted production file.