目 录CONTENT

文章目录

配置Docker代理-安装Mongodb

Administrator
2025-03-13 / 0 评论 / 0 点赞 / 5 阅读 / 0 字

要让Docker使用Clash代理访问国外Docker镜像源拉去Mongodb

配置Docker的代理设置

  1. 首先,创建或编辑Docker的服务配置目录:

mkdir -p /etc/systemd/system/docker.service.d
  1. 创建一个名为http-proxy.conf的配置文件:

vi /etc/systemd/system/docker.service.d/http-proxy.conf
  1. 在文件中添加以下内容(根据您的Clash配置调整):

[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1"
  1. 重新加载systemd配置:

systemctl daemon-reload
  1. 重启Docker服务:

systemctl restart docker
  1. 验证配置是否生效:

systemctl show --property=Environment docker

此命令应该显示您设置的代理环境变量。

[root@123 mongoDB]# systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://127.0.0.1:7890 HTTPS_PROXY=http://127.0.0.1:7890
[root@123 mongoDB]# 
  1. 现在尝试重新拉取MongoDB镜像:

docker pull mongo

拉取MongoDB镜像

[root@123 mongoDB]# docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
5a7813e071bf: Downloading [==========>              ]  6.138MB/29.75MB
073d1958f55c: Download complete 
25459f85dd50: Download complete 
2a9aeb311ccd: Download complete 
e8760a65b52a: Download complete 
7c39481ab08c: Download complete 
Digest: sha256:36f9c7390e7fdc734501d7797a88b9b661c1f0d1d2a64a1706dfb6ae3ffcef04
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

DB]# docker compose up -d
[+] Running 7/9
 ⠙ mongo [⣿⣿⣿⣿⣿⣿⠀⣿] 55.79MB / 270MB   Pulling                                                                           29.0s 
   ✔ 9cb31e2e37ea Pull complete                       15.7s 
   ✔ 792781e82b7e Pull complete                       15.7s 
   ✔ 62a4bc384409 Pull complete                       15.9s 
   ✔ b93dcffdc78a Pull complete                       15.9s 
   ✔ fa6d6e54530d Pull complete                       15.9s 
   ✔ f677c883e26c Pull complete                       15.9s 
   ⠙ 9183fab30761 Downloading  [============>      ]  23.6...      22.0s 
   ✔ 1354d0344319 Download complete                   12.9s 

临时设置代理

如果您使用的是非systemd的Docker安装或者想要临时设置代理,也可以通过环境变量直接设置:

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
docker pull mongo

如果以上方法不起作用,您也可以考虑修改Docker的镜像源为国内源:

  1. 编辑或创建Docker的daemon.json文件:

vi /etc/docker/daemon.json
  1. 添加以下内容使用阿里云镜像:

{
  "registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]
}
  1. 重启Docker服务:

systemctl restart docker

这样可以避免直接访问国外源,减少对代理的依赖。

安装MongoDB

配置mongodb持久化目录必须文件

/opt/MongoDB# cat 1.sh 
cd /opt/MongoDB

mkdir data             # 数据存储
mkdir data/db          # db 数据
mkdir data/log         # 日志存放
touch Dockerfile       # Docker 镜像文件
touch init.js          # 创建 Mongo 用户的脚本
touch docker-compose.yml  # Docker Compose 文件

生成TLS证书

#公网服务器
openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/MongoDB/mongodb.key -x509 -days 365 -out /opt/MongoDB/mongodb.crt -subj "/CN=服务器IP"

#本地虚拟机方式
openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/MongoDB/mongodb.key -x509 -days 365 -out /opt/MongoDB/mongodb.crt -subj "/CN=localhost"


cat /opt/MongoDB/mongodb.key /opt/MongoDB/mongodb.crt > /opt/MongoDB/mongodb.pem


openssl genrsa -out ssl/ca.key 4096
openssl req -x509 -new -nodes -key ssl/ca.key -sha256 -days 3650 -out ssl/ca.pem

docker-compose.yml

services:
  mongo:
    image: mongo:7.0
    container_name: mongodb
    command: [
      "mongod",
      "--auth",
      "--bind_ip_all",
      "--tlsMode", "preferTLS",
      "--tlsCertificateKeyFile", "/etc/mongodb/mongodb.pem",
      "--tlsCAFile", "/etc/mongodb/ca.pem",
      "--tlsAllowConnectionsWithoutCertificates"
    ]
    ports:
      - "27117:27017"
    volumes:
      - ./data:/data/db
      - ./mongodb.pem:/etc/mongodb/mongodb.pem:ro
      - ./ssl/ca.pem:/etc/mongodb/ca.pem:ro
    environment:
      - MONGO_INITDB_ROOT_USERNAME=adminuser
      - MONGO_INITDB_ROOT_PASSWORD=Comp1ex_P@ssw0rd!
    restart: always

安装MongoDB Shell

创建MongoDB Shell仓库文件

# 创建MongoDB Shell仓库文件
cat > /etc/yum.repos.d/mongodb-org-shell.repo << EOF
[mongodb-org-shell]
name=MongoDB Shell Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-shell/packages/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF

# 安装MongoDB Shell
yum install -y mongodb-mongosh

连接mongoDB

mongo --host 127.0.0.1 --port 27117 -u adminuser -p 'Comp1ex_P@ssw0rd!' --authenticationDatabase admin

mongosh --host 127.0.0.1 --port 27117 -u adminuser -p 'Comp1ex_P@ssw0rd!' --authenticationDatabase admin

-.-

0

评论区