#create .env in vps for docker compose
4 messages · Page 1 of 1 (latest)
Hi @dull jay. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To create a .env file for a Nuxt application running in a Docker container on a VPS, you have several options:
Option 1: Mount a local .env file into the container
In your docker-compose.yml file, you can mount a local .env file from your VPS into the container:
services:
nuxt-app:
image: your-nuxt-image
volumes:
- ./.env:/app/.env
# other configuration
This approach is shown in one of the examples from the knowledge sources where they mount the .env file like this:
volumes:
- ./.env/:/src/.env
Option 2: Set environment variables directly in docker-compose
Instead of using a .env file, you can set the environment variables directly in your docker-compose.yml:
services:
nuxt-app:
image: your-nuxt-image
environment:
NUXT_PUBLIC_API_BASE: "<https://api.example.com">
NUXT_API_SECRET: "your-secret-key"
# other configuration
This approach is recommended in the Stack Overflow post where they suggest:
environment:
NUXT_PUBLIC_STRAPI_BASE_URI: "blablabla"
Important notes about environment variables in Nuxt
-
Development vs Production:
.envfiles are automatically loaded during development and build time, but not in production. As the Nuxt documentation states:"After your server is built, you are responsible for setting environment variables when you run the server. Your
.envfiles will not be read at this point."
Nuxt Docs - .env -
Runtime Config: For production, you should use Nuxt's runtime config system and set the environment variables when running the container:
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
apiSecret: '', // can be overridden by NUXT_API_SECRET environment variable
public: {
apiBase: '' // can be overridden by NUXT_PUBLIC_API_BASE environment variable
}
}
})
-
Docker CMD: When starting your container, you can pass environment variables to the Node.js process:
CMD ["node", "/app/server/index.mjs"]Or with environment variables:
ENV NUXT_PUBLIC_API_BASE=<https://api.example.com> ENV NUXT_API_SECRET=your-secret-key