mongodb docker error: ERROR: child process failed, exited with 1

原创文章
声明:作者声明此文章为原创,未经作者同意,请勿转载,若转载,务必注明本站出处,本平台保留追究侵权法律责任的权利。
全栈老韩
全栈工程师,擅长iOS App开发、前端(vue、react、nuxt、小程序&Taro)开发、Flutter、React Native、后端(midwayjs、golang、express、koa)开发、docker容器、seo优化等。

背景

这个问题出现的背景是我使用docker compose yml文件来部署mongodb数据库等服务,在自己电脑上使用docker以及yml文件都没有问题,但是部署到云服务器上容器就一直在restart,链接数据库的后端服务也没有启动成功。

问题

mongodb | {"t":{"date":"2025-10-26T00:01:41.215+00:00"},"s":"I", "c":"CONTROL", "id":8423404, "ctx":"initandlisten","msg":"mongod shutdown complete","attr":{"Summary of time elapsed":{"Statistics":{"enterTerminalShutdownMillis":0,"stepDownReplCoordMillis":0,"quiesceModeMillis":0,"stopFLECrudMillis":0,"shutDownMirrorMaestroMillis":0,"shutDownWaitForMajorityServiceMillis":0,"shutDownGlobalConnectionPoolMillis":0,"shutDownSearchTaskExecutorsMillis":0,"shutDownFlowControlTicketHolderMillis":0,"shutDownReplicaSetMonitorMillis":0,"shutDownTransportLayerMillis":0,"shutDownTTLMonitorMillis":0,"shutDownExpiredDocumentRemoverMillis":0,"shutDownOtelMetricsMillis":0,"shutDownFTDCMillis":0,"shutDownOCSPMillis":0,"shutdownTaskTotalMillis":0}}}} mongodb | {"t":{"date":"2025-10-26T00:01:41.215+00:00"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":100}}
mongodb | ERROR: child process failed, exited with 1
mongodb | To see additional information in this output, start without the "--fork" option.
mongodb | Take a look at your mongod configuration to see if something is wrong.
mongodb | Could not init database.
mongodb | ['/usr/bin/mongod', '--bind_ip', '127.0.0.1', '--port', '27017', '--logappend', '--tlsMode', 'disabled', '--logpath', '/proc/1/fd/1', '--fork']
mongodb | Subprocess failed with errorcode 1

截图:
mongodb docker error

我的docker compose yml中关于mongodb的配置部分:

docker-compose.yml 复制代码
...
services:
  db: 
    image: mongodb/mongodb-community-server
    container_name: mongodb
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ./data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=xxx
...

这部分的mongodb配置是没有问题的,在自己的电脑上docker文件也没有问题,后面部署到服务器上也是可以的。

解决方法

根本原因是文件的权限问题,是哪个文件的问题呢?除了上面bind部分的data文件夹,其实还有yml文件的权限问题。

1. 首先查看服务器上文件的权限

docker-centos-server 复制代码
[root@VM-4-14-centos docker]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 26 08:01 data
-rw-r--r-- 1 root root 1174 Oct 26 07:54 docker-compose.yml

2. 修改文件夹和文件的权限

从上面看的出,挂载的data文件夹以及yml文件,权限并没有完全给予所有group user读写,那么我们需要改写它们的权限。

docker-centos-server 复制代码
[root@VM-4-14-centos docker]# chmod 777 *
[root@VM-4-14-centos docker]# ll
total 8
drwxrwxrwx 2 root root 4096 Oct 26 08:01 data
-rwxrwxrwx 1 root root 1174 Oct 26 07:54 docker-compose.yml

如上所示,使用chmod来给目录下所有文件修改为所有角色都有读写权限。

3. 在服务器上执行docker compose命令验证一下

docker-centos-server 复制代码
[root@VM-4-14-centos docker]# docker-compose up
Creating mongodb ... done
Attaching to mongodb
...
mongodb | {"t":{"$date":"2025-10-26T00:07:47.565+00:00"},"s":"I",  "c":"WTCHKPT",  "id":22430,   "ctx":"Checkpointer","msg":"WiredTiger message","attr":{"message":{"ts_sec":1761437267,"ts_usec":565072,"thread":"117:0x7f69ed371640","session_name":"WT_SESSION.checkpoint","category":"WT_VERB_CHECKPOINT_PROGRESS","log_id":1000000,"category_id":7,"verbose_level":"INFO","verbose_level_id":0,"msg":"saving checkpoint snapshot min: 3, snapshot max: 3 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 7"}}}
...

没有任何报错,至此我们知道了,需要在自动部署脚本中需要加入修改文件权限的脚本。

我截取部分自己写的部署脚本给一个示例:

deploy-shell-script.sh 复制代码
...
echo "执行docker-compose命令,启动容器"
ssh -i /Users/xxx/ssh_cert/ssh_xxx_visit.pem root@xxx.xxx.xxx.xxx '
echo "当前目录:";
pwd;
cd /root/xxx/docker;
chmod 777 *;
docker-compose up;
'
...

好了,至此问题解决了,希望对大家有帮助。

暂无评论,快来发表第一条评论吧