...
Code Block | ||||
---|---|---|---|---|
| ||||
FROM ubuntu:trusty #shell mode CMD ping localhost |
Build it with docker built build -t demo (if you don't use the -t <name> you will have to use docker tag afterwards).
Then we execute docker run -t demo
...
Code Block | ||||
---|---|---|---|---|
| ||||
FROM ubuntu:trusty #exec mode CMD ["ping", "localhost"] |
Build it with docker built build -t demo .
Then we execute docker run -t demo
...
- docker run demo hostname
- overrides the CMD value in the Dockerfile
- returns the host's name
- overrides the CMD value in the Dockerfile
- docker run demo ls -al
- overrides the CMD values in the Dockerfile
- lists all the files in the landing directory, notice the param "-al" is passed to the "ls" command
- docker run --entrypoint hostname demo
- returns the host's name
- docker run --entrypoint ls demo -al
- lists all the files in the landing directory, notice the param "-al" is passed to the "ls" command
...
Remember that if we use the following syntax docker run -t <image> <params, ...>, this is a CMD override and not an ENTRYPOINT override. So if run you execute docker run -t demo bbc.co.uk, the bbc.co.uk value is replaces the CMD value of localhost. Overriding ENTRYPOINT command parameters cannot can be done without overriding the command and its parameters, see below.
Some useful tricks
Consider the following command docker run -it --entrypoint bash demo, this will run the image and shell you into the image. Tis works because the bash command expects no other parameters.
What about the following, create a text file called "inputfile.txt" in the host with the follwing entries /lib /bin /usr /sbin, all with line breaks after them, then execute the command docker run -it --entrypoint ls demo -al $(cat inputfile.txt). This cool little command will cat the inputfile.txt prodducing /lib<cr/lf> /bin<cr/lf> /usr<cr/lf> /sbin<cr/lf> and pass this as a parameter to the command, so the command would actually be docker run -it --entrypoint ls demo -al /lib<cr/lf> /bin<cr/lf> /usr<cr/lf> /sbin<cr/lf>
Conclusion and advice
Use CMD in conjunction with ENTRYPOINT
...