Reputation: 12445
FROM alpine:3.5
CMD ["echo", "hello world"]
So after building docker build -t hello .
I can run hello by calling docker run hello
and I get the output hello world
.
Now let's assume I wish to run ls
or sh
- this is fine. But what I really want is to be able to pass arguments. e.g. ls -al
, or even tail -f /dev/null
to keep the container running without having to change the Dockerfile
How do I go about doing this? my attempt at exec mode fails miserably... docker run hello --cmd=["ls", "-al"]
Upvotes: 1
Views: 390
Reputation: 36823
Q. But what I really want is to be able to pass arguments. e.g. ls -al, or even tail -f /dev/null to keep the container running without having to change the Dockerfile
This is just achieved with:
docker run -d hello tail -f /dev/null
So the container is running in background, and it let you to execute arbitrary commands inside it:
docker exec <container-id> ls -la
And, for example a shell:
docker exec -it <container-id> bash
Also, I recommend you what @BMitch says.
Upvotes: 0
Reputation: 264591
Anything after the image name in the docker run
command becomes the new value of CMD
. So you can run:
docker run hello ls -al
Note that if an ENTRYPOINT
is defined, the ENTRYPOINT
will receive the value of CMD
as args rather than running CMD
directly. So you can define an entrypoint as a shell script with something like:
#!/bin/sh
echo "running the entrypoint code"
# if no args are passed, default to a /bin/sh shell
if [ $# -eq 0 ]; then
set -- /bin/sh
fi
# run the "CMD" with exec to replace the pid 1 of this shell script
exec "$@"
Upvotes: 2