Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to a docker image

Tags:

docker

I run tests inside docker image and I need to pass custom arguments all the time.

When I put arguments after image name docker thinks that argument is image name.

docker run  -t -i image-name -s test.py
docker run  -t -i image-name -- -s test.py

Error:

Failed no image test_arena2.py

Docker version 1.11.2, build b9f10c9

like image 614
Daniil Iaitskov Avatar asked Oct 25 '16 11:10

Daniil Iaitskov


People also ask

Can we pass arguments to docker container?

When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

How do I pass args to Dockerfile build?

If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .

How do I run a command against a docker container?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.


1 Answers

You can build your Dockerfile with a combination of ENTRYPOINT and CMD instructions, which will let you run containers with or without arguments, e.g:

FROM ubuntu
ENTRYPOINT ["/bin/echo"]
CMD ["hello"]

That says the entrypoint is the echo command, and the default argument is hello. Run a container with no arguments:

> docker run temp
hello  

Run with arguments and they all get passed to the entrypoint command:

> docker run temp -s stackoverflow
-s stackoverflow
like image 90
Elton Stoneman Avatar answered Oct 01 '22 18:10

Elton Stoneman