Skip to content

Instantly share code, notes, and snippets.

@tonkla
Last active July 10, 2020 06:45
Show Gist options
  • Save tonkla/39649652c7ee5e35648fa6c17e4c510f to your computer and use it in GitHub Desktop.
Save tonkla/39649652c7ee5e35648fa6c17e4c510f to your computer and use it in GitHub Desktop.
Node.js + MongoDB + Docker
version: '3'
services:
app:
build: .
container_name: app
restart: always
ports:
- '8001:8001'
depends_on:
- mongodb
environment:
WAIT_HOSTS: mongodb:27017
mongodb:
container_name: mongodb
image: mongo:4.2-bionic
restart: always
volumes:
- /data/db:/data/db
ports:
- '27017:27017'
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USER
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASS
FROM node:12-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . ./
RUN npm run build
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.6.0/wait /usr/src/wait
RUN chmod +x /usr/src/wait
EXPOSE 8001
CMD /usr/src/wait && node dist/index.js
build:
rm -rf dist && tsc && gcloud builds submit --tag gcr.io/${projectId}/${module}
deploy:
gcloud run deploy ${module} --image gcr.io/${projectId}/${module} --platform managed --region asia-northeast1
bad:
make build && make deploy
import mongoose from 'mongoose'
function connect() {
const user = process.env.MONGO_USER || ''
const pass = process.env.MONGO_PASS || ''
const host = process.env.MONGO_HOST || ''
const port = process.env.MONGO_PORT || ''
const db = process.env.MONGO_DB || ''
mongoose
.connect(`mongodb://${user}:${pass}@${host}:${port}/${db}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
console.log(`🚀 MongoDB Started`)
})
.catch(err => {
console.log(err)
})
mongoose.connection.on('error', err => {
console.log(err)
})
}
export default {
connect,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment