Techie
Techie

Reputation: 829

How to pass dynamic values to Docker container?

I am running a perl script in Dockerfile and I would like to pass dynamic command line arguments to the perl script while running docker image(container). Ex: CMD perl test.pl <args>. I am new to Docker.

Is there any possible way to pass dynamic values to the docker container like docker run <image name> <args>?

Upvotes: 4

Views: 7087

Answers (2)

heldic
heldic

Reputation: 405

You could use an Entrypoint script:

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

and

If the image also specifies an ENTRYPOINT then the CMD or COMMAND get appended as arguments to the ENTRYPOINT.

So depending on your Dockerfile you'd have something like this (python sample app):

FROM jfloff/alpine-python:3.6

# add entrypoint script
USER root
COPY start.sh /
RUN chmod a+x /start.sh

ENTRYPOINT ["/start.sh"]
CMD ["arg1"]

and start.sh:

#!/bin/bash

echo $1

# don't exit
/usr/bin/tail -f /dev/null

Now you can do something like:

15:19 $ docker run f49b567f05f1 Hello
Hello
15:21 $ docker run f49b567f05f1
arg1

Now if your script is set up to take those arguments, you should be able to run it as you want. Reference from Docker is attached, search for "Overriding Dockerfile image defaults" in this and then look in the CMD section.

Or check this Post.

Upvotes: 3

b0gusb
b0gusb

Reputation: 4721

I am not sure whether you can do it with CMD but if you just want to execute the perl script with some passed in arguments use ENTRYPOINT.

ENTRYPOINT ["perl", "test.pl"]
CMD ["default-arg"]

Run the container with:

docker -run <image-name> overriding-arg

Upvotes: 2

Related Questions