Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"CMD ['/home/user/script.sh']" in docker file doesn't work with docker-compose

I have a Docker file, thats builds perfectly with: docker build -t myfile . But when I trying to run it with docker-compose - it gives me an error:

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)

If I start this script manually - it works fine.

Dockerfile looks like:

FROM ubuntu:14.04

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

WORKDIR /home/root/myproject/

# Some other stuff...

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

docker-compose.yml:

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

Why I getting this error? Thank you.

like image 899
boxeus Avatar asked Oct 20 '15 05:10

boxeus


Video Answer


1 Answers

I simplified your example a bit

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

and used:

docker-compose.yml

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

find .

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

docker build & run

docker-compose build
docker-compose run web

and of course I get

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

assuming this is since there is no uwsgi.sh in the directory myproject.

If I add uwsgi.sh with

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

and test it with

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

it's there and behaves as expected:

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# 

but for docker-compose run web I still get

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

If I add a single blank to the Dockerfile CMD line:

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

the result is: /bin/sh: 1: [: /home/root/myproject/uwsgi.sh,: unexpected operator

which brings us closer. As a next step I am leaving out the "start" parameter.

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

this now leads to no output at all ...

If i change the CMD line to:

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

I get

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

and now you could continue in the Edison like approach:

I have not failed. I've just found 10,000 ways that won't work. until you find

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

which brings you closer with the result:

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

CMD expects an executable as the first parameter e.g.

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

has

CMD python app.py

as an example. To run your shell script you'll need a shell like bash. See also https://stackoverflow.com/a/33219131/1497139

like image 170
Wolfgang Fahl Avatar answered Sep 20 '22 16:09

Wolfgang Fahl