简体   繁体   English

使用RUN命令在Docker中执行Shell脚本

[英]Executing a shell script within docker with RUN command

New to dockers, so please bear with me. Docker的新手,所以请多多包涵。

My Dockerfile contains an ENTRYPOINT: 我的Dockerfile包含一个ENTRYPOINT:

ENV MONGOD_START "mongod --fork --logpath /var/log/mongodb.log --logappend --smallfiles"
ENTRYPOINT ["/bin/sh", "-c", "$MONGOD_START"]

I have a shell script add an entry to database through python script, and starts the server. 我有一个Shell脚本通过python脚本向数据库添加一个条目,然后启动服务器。

The script startApp.sh 脚本startApp.sh

chmod +x /addAddress.py
python /addAddress.py $1
cd /myapp/webapp 
grunt serve --force

Now, all the below RUN commands are unsuccessful in executing this script. 现在,以下所有RUN命令都无法成功执行此脚本。

sudo docker run -it --privileged myApp -C /bin/bash && /myApp/webapp/startApp.sh loc

sudo docker run -it --privileged myApp /myApp/webapp/startApp.sh loc

The docker log of container is 容器的docker日志是

"about to fork child process, waiting until server is ready for connections. forked process: 7 child process started successfully, parent exiting "

Also, the startApp.sh executes fine when I open a bash prompt in docker and run it. 另外,当我在docker中打开bash提示并运行它时,startApp.sh可以很好地执行。

I am unable to figure out what wrong I am doing, help please. 我无法弄清楚我在做什么错,请帮忙。

I guess (and I might be wrong, since I'm neither a MongoDB nor a Docker expert) that your combination of mongod --fork and /bin/sh -c is the culprit. 我猜(由于我既不是MongoDB也不是Docker专家,我可能错了,因为我不是mongod --fork/bin/sh -c才是罪魁祸首。

What you're essentially executing is this: 您实际上要执行的是:

/bin/sh -c mongod --fork ...

which 哪一个

  • executes a shell 执行一个shell
  • this shell executes a single command and waits for it to finish 这个shell执行一个命令并等待它完成
  • this command launches MongoDB in daemon mode 此命令以守护程序模式启动MongoDB
  • MongoDB forks and immediately exits MongoDB分叉并立即退出

The easiest fix is probably to just use 最简单的修复方法可能是只使用

 CMD ["mongod"]

like the official MongoDB Docker does. 就像官方的MongoDB Docker一样。

I would suggest you to create an entrypoint.sh file: 我建议您创建一个entrypoint.sh文件:

#!/bin/sh

# Initialize start DB command
# Pick from env variable MONGOD_START if it exists
# else use the default value provided in quotes
START_DB=${MONGOD_START:-"mongod --fork --logpath /var/log/mongodb.log --logappend --smallfiles"}

# This will start your DB in background
${START_DB} &

# Go to startApp directory and execute commands
`chmod +x /addAddress.py;python /addAddress.py $1; \
               cd /myapp/webapp ;grunt serve --force`

Then modify your Dockerfile by removing the last line and replacing it with following 3 lines: 然后通过删除最后一行并将其替换为以下3行来修改Dockerfile:

COPY entrypoint.sh /

RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

Then rebuild your container image using 然后使用

docker build -t NAME:TAG .

Now you run following command to verify if ENTRYPOINT is /entrypoint.sh 现在,您运行以下命令来验证ENTRYPOINT是否为/entrypoint.sh

docker inspect NAME:TAG | less

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM