Search code examples
node.jsdockerdocker-composesveltesapper

Connect mongo and sapper server with docker


I am working in a sapper / svelte project and I need to build the sapper project and connect it to a mongodb (I need to start mongo compose from docker-compose.yml)

At the moment I was trying to connect the db to the local mongo on port localhost: 27017 but it can't establish the connection. What should I do? Here there is my docker-compose

version: "3.4"

services:
  myapp:
    image: my_image
    deploy:
      update_config:
        delay: 30s
        parallelism: 1
        failure_action: rollback
    ports:
        - "3000:3000"

and here my dockerfile

FROM node:lts-alpine

WORKDIR /app

COPY static static
COPY emails emails

COPY package.json .
ENV NODE_ENV production
RUN npm install

COPY __sapper__/build __sapper__/build

EXPOSE 3000

CMD ["node", "__sapper__/build/index.js"]

Also what should I do to start the mongo deployment directly from compose? I have mongo on docker but I should start both directly from compose.


Solution

  • version: "3.4"
    
    services:
      app:
        image: yourimage
        ports:
            - "3000:3000"
        environment: 
            - MONGODB_URL=mongodb://yourip/yourdb     
      mongodb:
        image: mongo
        restart: always
        ports:
          - "yourportsdb:yourportsdb"
    

    it is not necessary to authenticate the mongo with password and user, eventually it passes the environments as suggested @Jihoon Yeo