This short note covers container arguments by going through some use cases. First we create a test container
FROM ubuntu:latest
COPY ./entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/usr/bin/top", "-H"]
And an entrypoint.sh
file
#!/usr/bin/env bash
echo "number of args: $#"
echo "Your container args are: $@"
for _i in $@; do
echo "param: $_i"
done
sleep 5
exec "$@"
Now we can build our image
# build our test image
docker build -t test_param_img .
And spin a container
# run our test container
docker run -it --rm --name param_test test_param_img
This will have the following output:
number of args: 2
Your container args are: /usr/bin/top -H
param: /usr/bin/top
param: -H
top - 10:09:44 up 2:31, 0 users, load average: 0.00, 0.04, 0.06
Threads: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.7 us, 0.7 sy, 0.0 ni, 98.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 31993.7 total, 28648.9 free, 1165.0 used, 2179.7 buff/cache
MiB Swap: 43008.0 total, 43008.0 free, 0.0 used. 30229.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 7360 3252 2680 R 0.0 0.0 0:00.06 top
Notice that CMD is the one passing parameters here, we have 2 parameters "/usr/bin/top" and "-H".
Now we will pass a parameter to docker run
docker run -it --rm --name param_test test_param_img -v
This time the output will be:
number of args: 1
Your container args are: -v
param: -v
/entrypoint.sh: line 10: exec: -v: invalid option
exec: usage: exec [-cl] [-a name] [command [argument ...]] [redirection ...]
So when you pass a parameter on the command line, you overwrite the docker file CMD. this is trying to run "-v" which produces an error. I could run the same command, but change '-v' for '-h' which is a valid exec parameter and get a different output. You can test that.
Another parameter test, now running top
docker run -it --rm --name param_test test_param_img top -v
Output:
number of args: 2
Your container args are: top -v
param: top
param: -v
procps-ng 3.3.17
Usage:
top -hv | -bcEeHiOSs1 -d secs -n max -u|U user -p pid(s) -o field -w [cols]