Skip to content

[tips] : Install the project with docker #36

@dubar-jeremy

Description

@dubar-jeremy

Here is a simple way how to install the project using docker :

Step 1
in the connection.js file, add the property : useUnifiedTopology: true to the url connection :

await mongoose.connect(databaseUrl, { useNewUrlParser: true, useUnifiedTopology: true })

Step 2
At the root level of the project, create a DockerFile :

# Use the official Node.js 12 image as base image
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["npm", "run", "server"]

Step 3
At the root level of the project, create a docker-compose.yml

version: '3'

services:
  # Node.js application service
  app:
    build:
      context: .
    ports:
      - "3001:3001"
    depends_on:
      - mongo
    environment:
      DATABASE_URL: "mongodb://mongo/argentBankDB"  # Use the service name 'mongo' as the hostname

  # MongoDB service
  mongo:
    image: mongo
    user: '502:502'
    ports:
      - "27017:27017"
    volumes:
      - ./data:/data/db:rw

volumes:
  mongo:
    external: true

The line user: '502:502 is needed to give you the right to copy the data folder from your container in your local machine.
You probably need to change the ID by your own id (on mac os you can get it by running : id -u in a terminal)
`Step 4

run docker-compose up

Capture d’écran 2024-01-18 à 23 18 08

Step 5
At this step your project is running (api + mongodb).
Now you need to populate the database. Here is how you can do :

  • Get your container ID by running docker container ls
  • Run the following command : docker exec -it 00460714d1d9 bash
  • Then you can just run the command npm run populate-db

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions