Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line arguments to Docker CMD

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?

like image 276
user14717 Avatar asked Sep 13 '15 20:09

user14717


People also ask

What does CMD command do in docker?

Docker CMD defines the default executable of a Docker image. You can run this image as the base of a container without adding command-line arguments. In that case, the container runs the process specified by the CMD command.

How do I run a docker container in CMD?

How to Use the docker run Command. The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images.

Can I use CMD and ENTRYPOINT?

However, if you use ENTRYPOINT instruction in executable form, you can set additional parameters using the CMD instruction. Depending on your requirements and use-cases, you can adopt any one of these commands.


1 Answers

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.

like image 155
Adrian Mouat Avatar answered Oct 04 '22 09:10

Adrian Mouat