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
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
docker run -i is good to be the begin of your command line then set of arguments for the run command only then at the very last -t image with image name provided
Image name should always be at the end of docker run command i.e. as last parameter to the command. Are you fine with passing the values as environment variable using below command
docker run -e "ENV_VAR_NAME=VALUE" -it image_name
Related
I have a Dockerfile in which I have specified an ENTRYPOINT "my_script.sh".In my_script.sh,I am exe.cuting a CURL command.When the docker image with this Dockerfile is built.How should I run it so that output of my_script.sh will be printed on my host.
Dockerfile -
FROM my-company-repo-java-base-image
ADD my_script.sh /root
ENTRYPOINT bash "/root/my_script.sh
my_script.sh
echo "Hello My Script"
curl -x POST "some_api_which_returns_json"
I have built the image using command
docker build
I want to run this image and see output of my_script.sh on my dockerhost.
Given a Docker image whose tag is $DOCKER_IMAGE:
docker container run -it --rm $DOCKER_IMAGE
-i keeps STDIN open
-t allocates a pseudo-TTY
--rm automatically removes the container when it exits
See docker container run for all the options.
Of course you can see the output of shell script. Make sure you delete the old image before building new one when you change the script. Else, your container will keep using the old script over and over. Here's an example
Dockerfile
FROM alpine:3.7
ENTRYPOINT ["/usr/bin/myscript.sh"]
COPY myscript.sh /usr/bin/myscript.sh
myscript.sh
#!/usr/bin/env sh
echo "Hello there"
commands to run:
docker image rm testdocker
docker build --tag testdocker .
docker run testdocker
You should see the line Hello there appears on the terminal
I start
docker run --rm -it -v $(pwd):/data -p 8080:80 klokantech/tileserver-gl --verbose
I took this in a Dockerfile:
FROM klokantech/tileserver-gl:v2.2.0
ADD . /data
But how can I call the klokantech/tileserver-gl option "--verbose" in Dockerfile?
If your option "--verbose" is static then you have to add it as ENV in dockerfile,
else if you options is dynamic then you have to use ARG command in dockerfile,so that you can pass argument while building docker image without changing dockerfile
ref : https://docs.docker.com/engine/reference/builder/#arg
In case of "klokantech/tileserver-gl --verbose" the argument is used by an implicit startscript on boot of the image. This argument is not in Dockerfile.
docker build -t "test:myimage" .
docker run test:myimage --verbose
My script is as follows:
# start a ubuntu container in the background
docker run -it --name ub -d ubuntu /bin/bash
sleep 1
# run a command in the container
docker exec -it ub bash
echo 234
# exit the container
exit
sleep 1
# do something else
echo 123
But the script would just stop right after exit and hang there. Does anyone know why is that?
p.s: My Docker version is: 17.03.0-ce, build 60ccb22
You have given -it during the run command. which opens up the /bin/bash of your container and waits there. The next command wont get executed until the first command execution is completed.
It's better to create a script file and move it inside the container while making the docker. and run the script on starting the docker. You may specify that using a CMD in the docker file.
You won't be needing an additional exec command.
The corresponding Dockerfile would be
FROM ubuntu:latest
COPY <path-to-script> <dest>
CMD [" <path-to-script> "]
You have to create the script file along with the Dockerfile. Build the docker using the command
docker build -t <image-name> <location of Dockerfile>
The execution command would be
docker run -d --name <name> -d ubuntu <path-to-script>
The Entrypoint of a docker image can be modified while running the image using --entrypoint in docker run command. I want to start a script in my image with some arguments at startup. I can get docker to run the script at startup as
docker run -it --rm --entrypoint /my/script/path.sh my-docker-image
How do I pass arguments to my script?
Note that I cannot modify the original dockerfile with which this image was created. Neither do I want to create another docker image with this image as its base.
When your Docker image has an ENTRYPOINT, either via a Dockerfile or provided on the command line with --entrypoint, any arguments on the docker run command line after the image name are passed to the entrypoint script.
So for example, if I have a script like this in myscript.sh:
#!/bin/sh
echo "Here are my arguments: $#"
And I run an image like this:
$ chmod 755 myscript.sh
$ docker run -it --rm -v $PWD/myscript.sh:/myscript.sh \
--entrypoint /myscript.sh alpine one two three
I will see the output:
Here are my arguments: one two three
...and the container will exit, because the entrypoint script didn't arrange to do anything else. You could replace alpine here (which is a minimal docker image) with any other Docker image that has /bin/sh (so, most of them). For example:
$ docker run -it --rm -v $PWD/myscript.sh:/myscript.sh \
--entrypoint /myscript.sh centos one two three
Here are my arguments: one two three
Note that I'm using the -v argument in this example to mount a script on my host into the container, since I didn't want to create a new image for the purposes of this example. You could obviously bake a similar script into your image instead.
For details, read the ENTRYPOINT docs.
I have a python file called perf_alarm_checker.py, this python file requires two command line arguments: python perf_alarm_checker.py -t something -d something, the Dockerfile looks like this:
# Base image
FROM some base image
ADD perf_alarm_checker.py /perf-test/
CMD python perf_alarm_checker.py
How to pass the two command line arguments, -t and -d to docker run? I tried docker run -w /perf-test alarm-checker -t something -d something but doesn't work.
Use an ENTRYPOINT instead of CMD and then you can use command line options in the docker run like in your example.
ENTRYPOINT ["python", "perf_alarm_checker.py"]
You cannot use -t and -d as you intend, as those are options for docker run.
-t starts a terminal.
-d starts the docker container as a daemon.
For setting environment variables in your Dockerfile use the ENV command.
ENV <key>=<value>
See the Dockerfile reference.
Another option is to pass environment variables through docker run:
docker run ... -e "key=value" ...
See the docker run reference.
Those environment variables can be accessed from the CMD.
CMD python perf_alarm_checker.py -t $ENV1 -d $ENV2