Closed arealclimber closed 6 months ago
docker-compose.yml
version: "3.1"
services:
mongo:
image: mongo
restart: always
command: mongod --replSet rs0
networks:
- mongodb-net
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
networks:
mongodb-net:
driver: bridge
volumes:
mongo_data:
mongod.conf
replication:
replSetName: rs0
docker run --name tbd-db -d -p 27017:27017 -v /mongod.conf:/etc/mongo/mongod.conf mongo
會有檔案存取的問題:
docker run --name tbd-db -d -p 27017:27017 -v /mongod.conf:/etc/mongo/mongod.conf mongo
5ee439fc01412e2b956160b15e6aad6c3724cbaca6383ea4888b18b55f71af51 docker: Error response from daemon: Mounts denied: The path /mongod.conf is not shared from the host and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing. See https://docs.docker.com/desktop/mac for more info.
-
用 https://github.com/CAFECA-IO/tbd-backend/pull/325 的方法重新試一遍之後,拿到 replica set state log,但 tableplus 還連不上,但是輸入 http://127.0.0.1:27017/
可以拿到 It looks like you are trying to access MongoDB over HTTP on the native driver port.
從log 跟連線看起來沒問題,但是要找 tableplus 跟後端連線入口
rs0 [direct: primary] testdb> rs.status()
{
set: 'rs0',
date: ISODate('2024-02-01T10:06:00.196Z'),
myState: 1,
term: Long('1'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 1,
writeMajorityCount: 1,
votingMembersCount: 1,
writableVotingMembersCount: 1,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
lastCommittedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
appliedOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
durableOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
lastAppliedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
lastDurableWallTime: ISODate('2024-02-01T10:05:53.033Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1706781893, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-02-01T09:59:02.746Z'),
electionTerm: Long('1'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1706781542, i: 1 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1706781542, i: 1 }), t: Long('-1') },
numVotesNeeded: 1,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
newTermStartDate: ISODate('2024-02-01T09:59:02.816Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-02-01T09:59:02.849Z')
},
members: [
{
_id: 0,
name: 'f7efa7552798:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 564,
optime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
optimeDate: ISODate('2024-02-01T10:05:53.000Z'),
lastAppliedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
lastDurableWallTime: ISODate('2024-02-01T10:05:53.033Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1706781542, i: 2 }),
electionDate: ISODate('2024-02-01T09:59:02.000Z'),
configVersion: 46994,
configTerm: -1,
self: true,
lastHeartbeatMessage: ''
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1706781953, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1706781953, i: 1 })
}
taking 0.5 hrs remaining 2.5 hrs
更改完以下檔案後,依序執行指令
docker-compose down -v
docker-compose build
docker-compose up -d
docker ps
docker exec -it <CONTAINER_ID> bin/bash
mongosh
rs.status()
rs.initiate({
_id: "rs0",
members: [{ _id: 0, host: "mongodb:27017" }]
})
rs.status()
docker network inspect mongodb-net
docker network ls
Dockerfile
# Base image
FROM node:18
# Create app directory
WORKDIR /usr/src/app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source
COPY . .
# Copy the .env and .env.development files
COPY development.env ./
# Creates a "dist" folder with the production build
RUN npm run build
# Expose the port on which the app will run
EXPOSE 3001
# Start the server using the production build
CMD ["npm", "run", "start:prod"]
docker-compose.yml
version: '3.8'
services:
mongodb:
image: mongo:latest
command: mongod --replSet rs0 --bind_ip_all
ports:
- '27017:27017'
volumes:
- mongodb-data:/data/db
networks:
- mongodb-net
nestjs-app:
build: .
ports:
- '3001:80'
environment:
- MONGO_URI=mongodb://mongodb:27017/nestjs?replicaSet=rs0
depends_on:
- mongodb
networks:
- mongodb-net
networks:
mongodb-net:
name: mongodb-net
driver: bridge
volumes:
mongodb-data:
development.env
MONGO_PROTOCOL=mongodb
MONGO_USERNAME=
MONGO_PASSWORD=
MONGO_RESOURCE=mongodb
MONGO_DATABASE=nestjs
MONGO_PORT=27017
MONGO_URI=mongodb://mongodb:27017/nestjs?replicaSet=rs0
mongo.config.ts
import { registerAs } from '@nestjs/config';
export default registerAs('mongo', () => {
const protocol = process.env.MONGO_PROTOCOL;
const username = process.env.MONGO_USERNAME;
const password = encodeURIComponent(process.env.MONGO_PASSWORD);
const resource = process.env.MONGO_RESOURCE;
const database = process.env.MONGO_DATABASE;
const port = process.env.MONGO_PORT;
const URL = process.env.MONGO_URL;
const uri = `${protocol}://${resource}:${port}/${database}?retryWrites=true&w=majority`;
console.log('uri in registerAs', uri);
console.log('MONGO_URI in registerAs', URL);
return {
username,
password,
resource,
uri,
useNewUrlParser: true,
useUnifiedTopology: true,
};
});
common.module.ts
MongooseModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
// uri: config.get<string>('MONGO_URI'),
uri: config.get<string>('mongo.uri'),
}),
}),
URL: mongodb://localhost:27017
在 tbd-backend root directory 的 docker-compose.yml 改為:
version: '3.8'
services:
mongodb:
image: mongo:latest
command: mongod --replSet rs0 --bind_ip_all
ports:
- '27017:27017'
volumes:
- mongodb-data:/data/db
networks:
- mongodb-net
nestjs-app:
build: .
ports:
- '3001:80'
environment:
- MONGO_URI=mongodb://mongodb:27017/nestjs?replicaSet=rs0
depends_on:
- mongodb
networks:
- mongodb-net
nextjs-app:
build:
context: ../tidebit-defi-dev
dockerfile: Dockerfile
ports:
- '3000:3000'
environment:
- API_URL=http://nestjs-app:3001
depends_on:
- nestjs-app
networks: mongodb-net: name: mongodb-net driver: bridge
volumes: mongodb-data:
- 接著在 tbd-backend 底下執行指令,就能在 `http:localhost:3000` 執行登入、入金、開倉、更新倉位、關倉
- `docker-compose build`
- `docker-compose up -d`
https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/f4dbf454-7d10-4f3a-aaf6-8d2cb3beda93
https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/e5cf436d-6179-4270-98bc-73f314926dee
https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/a088d464-ea4b-4765-931b-52d23aff62c7
https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/f4f0065d-ff3f-4d49-a5d3-a621ec03a4b9
taking 6 hrs
rs.initiate({ _id: "rs0", members: [{ _id: 0, host: "127.0.0.1:27017" }] })
是導致後端容器連不上DB容器的原因,要讓 Docker 容器彼此連線的前提下,需要用“可尋址(addressable)”的設定,例如用<MONGO_SERVICE_NAME>:<PORT>
,用 127.0.0.1:27017
會找不到
在 docker 上運行 mongo db, nest.js, next.js ,登入後不能入金,Network request 回傳201,backend console 顯示以下錯誤訊息,餘額沒有增加,因為我沒有設定 replica set
MongoDB 設定