Generate .env.exmaple file for docker - node.js

I want to generate an .env and and an .env.example file and push it to the docker image during the docker build with Dockerfile.
The problem is that the .env.example file is not copied to the docker image. I think it works with the normal env, because I copy the env.production and save it as an env.
However, I am using node.js and typescript. Most people know that there is a way to use env viriables in the code by generating an env.d.ts and env.example.
During docker compose i am getting unfortunately that error.
no such file or directory, open '.env.example'
apparently the env.example is not copied to the dist folder. I need this one though.
Dockerfile
# stage 1 building the code
FROM node as builder
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
COPY .env.production .env
RUN npm run build
COPY .env.example ./dist
# stage 2
FROM node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /usr/app/dist ./dist
COPY ormconfig.docker.json ./ormconfig.json
EXPOSE 4000
CMD node dist/index.js
docker-compose.yaml
version: "3.7"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
build:
context: ./
dockerfile: ./Dockerfile
depends_on:
- db
ports:
- "4000:4000"
volumes:
- ./src:/src
exmaple how i am using env variables in my application
env.d.ts
declare namespace NodeJS {
interface ProcessEnv {
PORT: string
EMAIL: string
EMAIL_PASSWORD: string
}
}
env.exmaple (must be there)
PORT=
EMAIL=
EMAIL_PASSWORD=
index.ts
import 'dotenv-safe/config'
import express from 'express'
const app = express()
app.listen(process.env.PORT, () => {
console.log(`Server starten on port ${process.env.PORT}`)
})

Related

Docker compose for monorepo with only one package.js for client and server

I have a monorepo and I am user only once package.json for both client and server and I am running the app user env var.
This is my folder structure
and this is my docker compose file
version: '3.8'
services:
api:
container_name: api
build: ./src/api
ports:
- 8888:8888
volumes:
- .:/app
- /app/node_modules
environment:
- APP_ENV=staging
manager:
container_name: manager
build: ./src/manager
ports:
- 3000:3000
volumes:
- .:/app
- /app/node_modules
environment:
- APP_ENV=staging
this is my DockerFile for individual apps
FROM public.ecr.aws/docker/library/node:16.13.0
# ENV NODE_OPTIONS=--max-old-space-size=8192
RUN npm install -g npm#8.1.0
# Bundle application source.
RUN mkdir -p /usr/src/app
COPY ["package*.json", "../../"]
WORKDIR /usr/src/app
# Bundle application source.
COPY . /usr/src/app
# WORKDIR ../../
RUN npm cache clear --force
RUN npm install
RUN npm ci --production
COPY . .
EXPOSE 8888
CMD ["node", "../../index.js"]
and this is my index.js where i am running the apps using env variables
switch (process.env.ENTRYPOINT) {
case 'api':
await import('./src/api/index.js');
break;
case 'manager':
chdir('src/manager');
await import('./src/manager/server.js');
break;
}
the package.json have scripts something like this
"start:api": "ENTRYPOINT=api node index.js",
"start:manager": "ENTRYPOINT=manager node index.js",

getting a local docker-composed node / express & react app deployed on GCP using a single Dockerfile

I have a nodejs / express / react app running locally that starts a node server at :3001 and a react app at :3000 which can make requests to the express API.
I then made a /client/Dockerfile and a /server/Dockerfile, and a /docker-compose.yml which is capable of running my app locally without issue.
I now want to deploy this to GCP's Cloud Run, but GCP does not allow multiple docker images / docker-compose (AFAIK - it only has a single field for "Dockerfile"), so I am trying to reconfigure things to work with a single Docker file.
Here's what I had for the working, local instance:
client/Dockerfile
FROM node:lts-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]
server/Dockerfile
FROM node:lts-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3001
# You can change this
CMD [ "npm", "run", "dev" ]
docker-compose.yml
version: "3"
services:
client:
container_name: gcp-cloudrun-client
build:
context: ./client
dockerfile: Dockerfile
image: mheavers/gcp-cloudrun-client
ports:
- "3000:3000"
volumes:
- ./client:/usr/src/app
server:
container_name: gcp-cloudrun-server
build:
context: ./server
dockerfile: Dockerfile
image: mheavers/gcp-cloudrun-server
ports:
- "3001:3001"
volumes:
- ./server:/usr/src/app
How do I combine all this into a single Dockerfile and do away with docker-compose?
this was my final docker file to replace docker-compose:
FROM node:lts-slim AS client
WORKDIR /usr/src/app
COPY client/ ./client/
RUN cd client && npm install && npm run build
FROM node:lts-slim AS server
WORKDIR /root/
COPY --from=client /usr/src/app/client/build ./client/build
COPY server/package*.json ./server/
RUN cd server && npm install
COPY server/index.js ./server/
EXPOSE 80
CMD ["node", "./server/index.js"]

Cannot connect mongodb using docker, connection refused

I have the following dockerfile
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build
COPY . .
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 nodejs
RUN adduser -S 1001 nextjs
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1
ENV HOST=0.0.0.0
CMD yarn start
and the following docker-compose file
version: '3.5'
services:
ellis-development:
image: ellis-development
container_name: app
build:
context: .
dockerfile: dockerfile.dev
environment:
- NEXT_PUBLIC_ENV
- SENDGRID_API_KEY
- MONGODB_URI
- NEXTAUTH_SECRET
- NEXTAUTH_URL
ports:
- 3000:3000
volumes:
- .:/app
links:
- mongodb
depends_on:
- mongodb
mongodb:
image: mongo:latest
# environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: testing123
ports:
- 27017:27017
volumes:
- data:/data/db
volumes:
data:
I have all my envs setup in a .env file like so
NEXT_PUBLIC_ENV=local
SENDGRID_API_KEY=<redacted>
MONGODB_URI=mongodb://localhost:27017/<redacted>
NEXTAUTH_SECRET=eb141u85
NEXTAUTH_URL="http://localhost:3000"
This creates the following when running docker compose
and I can connect to localhost:27017 using mongodb compass.
However, for some reason docker cannot connect to my application.
What am I doing wrong here? First time setting up mongodb with docker so 🤷‍♂️
Fixed, spotted the error as soon as I posted the question... 😅
Changed
MONGODB_URI=mongodb://localhost:27017/<redacted>
to
MONGODB_URI=mongodb://mongodb:27017/<redacted>

Add .env.example in dist folder during docker compose up

I want to generate an .env and and an .env.example file and push it to the docker image during the docker build with Dockerfile.
The problem is that the .env.example file is not copied to the docker image. I think it works with the normal env, because I copy the env.production and save it as an env.
However, I am using node.js and typescript. Most people know that there is a way to use env viriables in the code by generating an env.d.ts and env.example.
During docker compose i am getting unfortunately that error.
no such file or directory, open '.env.example'
apparently the env.example is not copied to the dist folder. I need this one though.
Dockerfile
# stage 1 building the code
FROM node as builder
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
COPY .env.production .env
RUN npm run build
COPY .env.example ./dist
# stage 2
FROM node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /usr/app/dist ./dist
COPY ormconfig.docker.json ./ormconfig.json
EXPOSE 4000
CMD node dist/index.js
.dockerignore
node_modules
docker-compose.yaml
version: "3.7"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
build:
context: ./
dockerfile: ./Dockerfile
depends_on:
- db
ports:
- "4000:4000"
volumes:
- ./src:/src
Could somebody please help me !

Docker Compose File for Node.Js app that wraps a static frontend

I am new to Docker and I am trying to figure out how to setup a docker-compose file to build my node.js image and my frontend image
my Node.js App wraps my Vue.js Frontend and serves the static files.
Folder Structure
In my docker-compose.yml
root of project
version: '3'
services:
backend:
env_file:
"./app/frontend/.env"
build:
context: ./app
dockerfile: ./Dockerfile
image: "myusername/backend"
ports:
- "8000:8000"
frontend:
build:
context: ./app/frontend
dockerfile: ./Dockerfile
image: "myusername/frontend"
ports:
- "8080:8080"
links:
- "backend"
Backend DockerFile
/app
FROM node:12.18.3
LABEL version="1.0"
LABEL description="This is the base docker image for Backend"
LABEL maintainer = ["test#testuser.com"]
WORKDIR /app
COPY ["package.json", "package-lock.json", "./"]
RUN ls
RUN npm install --production
COPY . .
EXPOSE 8000
CMD ["node", "app.js"]
DockerFile for Frontend
/app/fronted
FROM node:12.18.3
LABEL version="1.0"
LABEL description="This is the base docker image for Frontend"
LABEL maintainer = ["test#testuser.com"]
WORKDIR /app/frontend
COPY ["package.json", "package-lock.json", "./"]
RUN npm install --production
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
I am not sure if this is correc, I can successfully build the images, but can't navigate to any of the ports I exposed
docker-compose config
does return the contents of my docker-compose.yml file
Note
at the root of /app and app/frontend
there is a .dockerignore file that just contains
node_modules

Resources