mongodb 연결

생성일
Dec 21, 2021 03:36 PM
태그
version: "3.7"
services:
  web:
    image: node:12-alpine
    working_dir: /app
    volumes:
      - ./web:/app
    command: sh -c "yarn install && yarn run dev"
    ports:
       - 3000:3000
  server:
    image: node:12-alpine
    working_dir: /app
    depends_on:
      - db
    volumes:
      - ./server:/app
    command: sh -c "yarn install && yarn run start:dev"
    ports:
       - 3001:3001
    environment:
      - DB_URL=mongodb://mongo/nest
  db:
    image: mongo
    container_name: mongo
    restart: always
    ports:
       - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: soso01
      MONGO_INITDB_ROOT_PASSWORD: testtest
      MONGO_INITDB_DATABASE: nest
 
// app.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [MongooseModule.forRoot(process.env.DB_URL)],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
 
mongoURL 주소를 mongodb://localhost:27017/nest로 하니 연결실패하고 container_name 으로 지정해주니 된다.
docker inspect
[
    {
        "Name": "notion_default",
        "Id": "6979673db38011e18b942e5941ba493649be8ff1bf7aeb7721db098c1dd97598",
        "Created": "2021-12-21T14:23:06.2178989Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.24.0.0/16",
                    "Gateway": "172.24.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1a8b7357d897ae312f5628259d7a6b0c0afa306e23cab60f98022fcca953f195": {
                "Name": "notion_web_1",
                "EndpointID": "f9442718721761dcf426ecee6097536cdba0c01b2b5cf3bb99e70fa20967eea2",
                "MacAddress": "02:42:ac:18:00:03",
                "IPv4Address": "172.24.0.3/16",
                "IPv6Address": ""
            },
            "2b1a65402ce4e12c1f11a95878f71e80a204444f845ec71d7208fd3df21bac08": {
                "Name": "notion_db_1",
                "EndpointID": "c70a76dfb00aa045cc11e388f29e3095a7c8d9e77591ca20d79049ab037b2ee4",
                "MacAddress": "02:42:ac:18:00:02",
                "IPv4Address": "172.24.0.2/16",
                "IPv6Address": ""
            },
            "b7f8fc35f361b9a57dc9f2a8577dc0950db0d1d9936bdec550771e0084a2045d": {
                "Name": "notion_server_1",
                "EndpointID": "a5f4bd9da9f9427ee2ec20bfbbe9033d329e982e1d7f0228e63acbfa64fd6934",
                "MacAddress": "02:42:ac:18:00:04",
                "IPv4Address": "172.24.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "notion",
            "com.docker.compose.version": "2.2.1"
        }
    }
]
inspect로 확인하니 server와 db 컨테이너의 ip 주소 자체가 다르다.
port 주소 매핑해주는건 docker-compose의 네트워크 외부와 내부 사이의 매핑이라서, 같은 도커 네트워크 내의 컨테이너끼리는 안되는듯 함. (서버의 localhost:27017이 db의 27017 포트와 연결안되는것 같음)
 

Loading Comments...