我正在尝试将微服务产品打包到一个容器中,包括所有代码和MongoDB数据库。
编写以下dockerfile:
FROM mongo:3.4-xenial
RUN mkdir -p /opt/xxx
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-get install zip \
unzip
RUN apt-get update -y && \
apt-get clean
COPY ./xxx-repo/target/xxx-repo.zip /opt/xxx
WORKDIR /opt/xxx
RUN unzip xxx-repo.zip
# Start MongoDB here
RUN mongod --logpath /var/log/mongodb.log --dbpath /data/db/ &
RUN echo ' \n\
mongo -- admin <<EOF \n\
db.createUser({user:"user", pwd:"admin123", roles:[{role:"root", db:"admin"}]}) \n\
EOF ' >> ./init_mongo.sh && chmod +x ./init_mongo.sh && ./init_mongo.sh
当我尝试此操作时,它返回:
---> Running in 7a21d6fed848
MongoDB shell version v3.4.22
connecting to: mongodb://127.0.0.1:27017/admin
2019-09-10T09:30:51.453+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2019-09-10T09:30:51.457+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:240:13
@(connect):1:6
exception: connect failed
The command '/bin/sh -c echo ' \n mongo -- admin <<EOF \n db.createUser({user:"user", pwd:"admin123", roles:[{role:"root", db:"admin"}]}) \nEOF ' >> ./init_mongo.sh && chmod +x ./init_mongo.sh && ./init_mongo.sh' returned a non-zero code: 1
我如何启动mongo实例,也许等到守护程序准备好后再继续提供服务?
答案 0 :(得分:1)
您不应在构建阶段启动Mongodb,如果这样做,它将在下一个RUN命令中不可用,因为每个RUN命令都在其自己的Shell中执行。
将其移动到cmd
# Start MongoDB here
RUN mongod --logpath /var/log/mongodb.log --dbpath /data/db/ &
并将这些行添加到.js
文件或bash中,并在DB更好时从入口点运行,以检查也处理导入和其他内容的官方图像入口点。
RUN echo ' \n\
mongo -- admin <<EOF \n\
db.createUser({user:"user", pwd:"admin123", roles:[{role:"root", db:"admin"}]}) \n\