feat: add mongo init script, use root in app image

This commit is contained in:
wancat committed 2023-07-31 17:19:03 +08:00
1 parent ec90c484b4
commit c3030b22be
5 files changed
+27 -12

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
vite.config.js vite.config.js
.gitignore .gitignore
data data
mongo-init.js
-6
View File
@@ -5,14 +5,8 @@ COPY package.json yarn.lock ./
COPY server/package.json ./server/ COPY server/package.json ./server/
RUN yarn install --frozen-lockfile --production RUN yarn install --frozen-lockfile --production
ARG DOCKER_GID=140
RUN groupadd -g $DOCKER_GID docker && usermod -aG docker node
WORKDIR /app/server WORKDIR /app/server
USER node
COPY server/src ./src COPY server/src ./src
COPY server/quests ./quests COPY server/quests ./quests
COPY server/swagger.yaml . COPY server/swagger.yaml .
+12 -2
View File
@@ -14,7 +14,7 @@ services:
volumes: volumes:
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
environment: environment:
- MONGO_URL=mongodb://db:27017/odyssey-test - MONGO_URL=mongodb://user:user-password@db:27017/odyssey-test
- DOCKER_NETWORK=linux-odyssey-players - DOCKER_NETWORK=linux-odyssey-players
db: db:
image: mongo image: mongo
@@ -24,9 +24,19 @@ services:
ports: ports:
- '27017:27017' - '27017:27017'
volumes: volumes:
- ./data:/data/db - mongo:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin-password
- MONGO_USER=user
- MONGO_PASS=user-password
- MONGO_DB=odyssey-test
networks: networks:
internal: internal:
players: players:
name: linux-odyssey-players name: linux-odyssey-players
volumes:
mongo:
+10
View File
@@ -0,0 +1,10 @@
const user = process.env.MONGO_USER
const password = process.env.MONGO_PASS
const dbName = process.env.MONGO_DB
db = db.getSiblingDB(dbName)
db.createUser({
user: user,
pwd: password,
roles: [{ role: 'readWrite', db: dbName }],
})
+4 -4
View File
@@ -6,7 +6,7 @@ const engine = new Docker()
const questPath = '/home/zeko/src/linux-odyssey/server/quests' const questPath = '/home/zeko/src/linux-odyssey/server/quests'
const containerOptions = { const containerOptions = {
Image: 'lancatlin/quest-helloworld', Image: 'lancatlin/linux-odyssey:helloworld',
AttachStdin: true, AttachStdin: true,
AttachStdout: true, AttachStdout: true,
AttachStderr: true, AttachStderr: true,
@@ -38,9 +38,9 @@ export async function getOrCreateContainer(id) {
...containerOptions, ...containerOptions,
name: id, name: id,
}) })
} catch (error) { } catch (err) {
console.log(error) console.log(err)
throw error throw err
} }
await network.connect({ Container: container.id }) await network.connect({ Container: container.id })