Something about Docker
Almost every 6 months, I would look up the differences between Docker ENTERPOINT and CMD. Now it comes to my blog
0 Setting Env variable in Linux
Starting with a sublt differences between two ways of setting env var
# Only exist in this shell
var = 1
# Exist in other processes
export var = 1
Another differences is that env
or printenv
can show var set by export
, but NOT directly =
assignment
1 Shell form vs Executable form
Shell form directly feed in commands
ENV name Darwin
CMD echo “Hello World”
ENTRYPOINT /bin/echo "Welcome, $name"
Executable mode directly runs the executable binaries, without going through shell validation and processing.
CMD ["yum", "-y", "install", "httpd"]
ENV name Darwin
# Welcome, $name
ENTRYPOINT ["/bin/echo", "Welcome, $name"]
# Welcome, Darwin
ENTRYPOINT ["/bin/bash", “-c” "echo Welcome, $name"]
2 ENTRYPOINT vs CMD
|-|ENTRYPOINT|CMD| |–|–|–| |order|First|Second| |Default|can NOT be overwrite in docker run CLI|CAN be overwrite in docker run CLI| |Ignore|will NOT be ignored| will be ignored with run parameters|
CMD ["Darwin"]
ENTRYPOINT ["echo", "Hello,"]
#docker run container
#output: Hello, Darwin
#
#docker run container Kevin
#output: Hello, Kevin # CMD ignored
3 ARG vs ENV
This picture explains all.
- ARG only exists during the build time
- ENV exists in both build and run time,so you can see it in both image(with
docker inspect
) and container.
A side note, the .env
file is used for docker-composer.yaml
to get env var values.
Both ARG and ENV can be reset by CLI