Docker文件中的“CMD [‘/home/user/script.sh’]”无法与docker-compose一起使用

前端之家收集整理的这篇文章主要介绍了Docker文件中的“CMD [‘/home/user/script.sh’]”无法与docker-compose一起使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个Docker文件,它完美地构建:docker build -t myfile.
但当我尝试使用docker-compose运行它时 – 它给了我一个错误

web_1 | /bin/sh: 1: [/home/root/myproject/uwsgi.sh: not found
myproject_web_1 exited with code 127
Gracefully stopping... (press Ctrl+C again to force)

如果我手动启动此脚本 – 它工作正常.

Dockerfile看起来像:

FROM ubuntu:14.04

ADD ./myproject/ /home/root/myproject/

WORKDIR /home/root/myproject/

# Some other stuff...

CMD ['/home/root/myproject/uwsgi.sh','start' ]

泊坞窗,compose.yml:

web:
  build: .
  ports:
   - "8888:8888"
  links:
   - db
db:
  image: MysqL

为什么我收到此错误
谢谢.

最佳答案
我简化了你的例子

https://github.com/BITPlan/docker-stackoverflowanswers/tree/master/33229581

和使用:

泊坞窗,compose.yml

web:
  build: .
  ports:
   - "8888:8888"

找 .

.
./docker-compose.yml
./Dockerfile
./myproject

码头工作者跑

docker-compose build
docker-compose run web

当然,我明白了

/bin/sh: 1: [/home/root/myproject/uwsgi.sh,: not found

假设这是因为myproject目录中没有uwsgi.sh.

如果我添加uwsgi.sh

echo 'echo $0 is there and called with params $@!' > myproject/uwsgi.sh
chmod +x myproject/uwsgi.sh

并测试它

docker-compose run web /bin/bash
ls
cat uwsgi.sh
./uwsgi.sh start

它就在那里并按预期行事:

root@9f06f8ff8c3b:/home/root/myproject# ls
uwsgi.sh
root@9f06f8ff8c3b:/home/root/myproject# cat uwsgi.sh 
echo $0 is there and called with params $@!
root@9f06f8ff8c3b:/home/root/myproject# ./uwsgi.sh start
./uwsgi.sh is there and called with params start!
 root@9f06f8ff8c3b:/home/root/myproject# 

但对于docker-compose运行网络,我仍然得到

/bin/sh: 1: [/home/root/myproject/uwsgi.sh,: not found

如果我向Dockerfile CMD行添加一个空格:

CMD [ '/home/root/myproject/uwsgi.sh','start' ] 

结果是:
    / bin / sh:1:[:/ home / root /myproject / uwsgi.sh,:意外的运算符

这让我们更接近.作为下一步,我将省略“start”参数.

CMD [ '/home/root/myproject/uwsgi.sh' ] 

现在这导致根本没有输出……

如果我将CMD线更改为:

CMD [ "/home/root/myproject/uwsgi.sh","start" ] 

我明白了

 Cannot start container 
 0b9da138c43ef308ad70da4a7718cb96fbfdf6cda113e2ae0ce5e24de06f07cd: [8]  
 System   error: exec format error

现在你可以继续像爱迪生一样:

我没有失败.我刚刚找到了10,000种不起作用的方法.
直到你找到

CMD [ "/bin/bash","/home/root/myproject/uwsgi.sh","start" ]

这会让你更接近结果:

 /home/root/myproject/uwsgi.sh is there and called with params start!

CMD期望可执行文件作为第一个参数,例如

https://docs.docker.com/compose/

具有

CMD python app.py

举个例子.要运行shell脚本,您需要一个像bash这样的shell.
另见https://stackoverflow.com/a/33219131/1497139

猜你在找的Docker相关文章