Loading

Docker 运行 MongoDB

Docker 运行 MongoDB

实验环境

  • 宿主机:CentOS Stream release 9
  • Docker:Docker Engine 27.3.1
  • MongoDB:7.0.14
  • Mongosh;2.3.1

配置文件

mongod.conf

[karma@localhost karmamongodb]$ cat mongod.conf | grep -vE "#|^$"
storage:
  dbPath: /var/lib/mongodb
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  port: 27017
  bindIp: 0.0.0.0
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

supervisord.conf

[karma@localhost karmamongodb]$ cat supervisord.conf 
[supervisord]
nodaemon=true

[program:mongodb]
command=/usr/bin/mongod --config /etc/mongodb/mongod.conf

[program:ssh]
command=/usr/sbin/sshd -D

Dockerfile

FROM ubuntu:20.04
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main universe" > /etc/apt/sources.list
RUN apt -yqq update && apt install -yqq ca-certificates
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main universe" > /etc/apt/sources.lista
RUN apt -yqq update && apt install -yqq supervisor
RUN apt install -yqq openssh-server openssh-client
RUN mkdir -p /var/run/sshd
RUN echo 'root:shiyanlou' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN apt install -yqq --allow-downgrades gpgv=2.2.19-3ubuntu2
RUN apt -yqq install gnupg curl
RUN curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor
RUN echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list
RUN apt-get -yqq update
RUN apt-get install -yqq mongodb-org
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY mongod.conf /etc/mongodb/mongod.conf
EXPOSE 27017 22
CMD ["supervisord"]

构建镜像

docker image build -t karmamongodb:0.1

运行容器

docker container run -P -d --name mongodb karmamongodb:0.1

查看容器

[karma@localhost karmamongodb]$ docker container ls
CONTAINER ID   IMAGE              COMMAND         CREATED          STATUS          PORTS                                                                                        NAMES
3fb90df4f853   karmamongodb:0.1   "supervisord"   26 minutes ago   Up 26 minutes   0.0.0.0:32794->22/tcp, [::]:32794->22/tcp, 0.0.0.0:32795->27017/tcp, [::]:32795->27017/tcp   mongodb

使用 mongosh 连接数据库

在宿主机上运行:

mongosh --host 127.0.0.1 --port 32795

或者

mongosh --host 172.17.0.2 --port 27017
posted @ 2024-09-25 16:03  杨谖之  阅读(16)  评论(0编辑  收藏  举报