Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass flags to the command that docker runs?

Tags:

docker

The documentation for the run command follows the following syntax:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

however I've found at times that I want to pass a flag to [COMMAND].

For example, I've been working with this image, where the [COMMAND] as specified in the Dockerfile is:

CMD ["/bin/bash", "-c", "/opt/solr/bin/solr -f"]

Is there any way to tack on flags to "/opt/solr/bin/solr -f" so that it's in the form "/opt/solr/bin/solr -f [-MY FLAGS]"?

Do I need to edit the DockerFile or is there some built in functionality for this?

like image 586
m0meni Avatar asked Jul 22 '15 20:07

m0meni


1 Answers

There is a special directive ENTRYPOINT which fits your needs. Unlike CMD it will add additional flags at the end of your command.

For example, you can write

ENTRYPOINT ["python"]

and run it with

docker run <image_name> -c "print(1)"

Note, that this only will work if you write command in exec form (via ["...", "..."]), otherwise ENTRYPOINT will invoke shell and pass your args there, not to your script.

More generally, you can combine ENTRYPOINT and CMD

ENTRYPOINT ["ping"]  
CMD ["www.google.com"]  

Where CMD means default args for your ENTRYPOINT. Now you can run both of

docker run <image_name>
docker run <image_name> yandex.ru

and only CMD will be replaced.

Full reference about how ENTRYPOINT and CMD interact can be found here

like image 188
Stanislav Tsepa Avatar answered Sep 30 '22 20:09

Stanislav Tsepa