Command line arguments to Docker CMD
Asked Answered
L

1

45

I want to pass in a parameter into a docker CMD. For example, if the contents of Dockerfile are

FROM ubuntu:15.04
CMD ["/bin/bash", "-c", "cat", "$1"]

Then I want to run as follows:

docker build -t cat_a_file .
docker run -v `pwd`:/data cat_a_file /data/Dockerfile

I would like the contents of Dockerfile to be printed to the screen. But instead, Docker thinks that /data/Dockerfile is a script that should override the CMD, giving the error

Error response from daemon: Cannot start container 7cfca4: 
[8] System error: exec: "/data/Dockerfile": permission denied

How can this be avoided?

Lagos answered 13/9, 2015 at 20:54 Comment(0)
R
48

Use ENTRYPOINT for stuff like this. Any CMD parameters are appended to the given ENTRYPOINT.

So, if you update the Dockerfile to:

FROM ubuntu:15.04
ENTRYPOINT ["/bin/bash", "-c", "cat"]

Things should work as you wish.

Also, as you don't need the $1, you should be able to change it to:

FROM ubuntu:15.04
ENTRYPOINT ["/bin/cat"]

I haven't tested any of this, so let me know if it doesn't work.

Recognizor answered 13/9, 2015 at 21:13 Comment(2)
Just so we can make more complicated scripts, what is the correct way to use the $1-style argument?Lagos
Probably best to use environment variables in those cases. You can't use $1 etc.Recognizor

© 2022 - 2024 — McMap. All rights reserved.