Skip to content

Instantly share code, notes, and snippets.

@ahobson
Last active July 15, 2020 20:56
Show Gist options
  • Save ahobson/02f0ad52809138345ac71023280c57b2 to your computer and use it in GitHub Desktop.
Save ahobson/02f0ad52809138345ac71023280c57b2 to your computer and use it in GitHub Desktop.
Dockerfile show interaction between entrypoint and cmd
# This is what the Dockerfile looks like
$ cat Dockerfile
FROM bash
ENTRYPOINT ["/bin/ls"]
CMD ["-l"]
# Build the image so we can experiment
$ docker build -t mybashls .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM bash
---> 330fdabba8e4
Step 2/3 : ENTRYPOINT ["/bin/ls"]
---> Running in deeed59492db
Removing intermediate container deeed59492db
---> 0b173f8cee5e
Step 3/3 : CMD ["-l"]
---> Running in f702e991cb96
Removing intermediate container f702e991cb96
---> ff41b00a51cf
Successfully built ff41b00a51cf
Successfully tagged mybashls:latest
# Run the image with the default entrypoint and cmd which winds up being "ls -l" INSIDE the container
$ docker run --rm mybashls
total 56
drwxr-xr-x 1 root root 4096 Apr 24 22:51 bin
drwxr-xr-x 5 root root 340 Jul 15 20:34 dev
drwxr-xr-x 1 root root 4096 Jul 15 20:34 etc
drwxr-xr-x 2 root root 4096 Apr 23 06:25 home
drwxr-xr-x 1 root root 4096 Apr 24 22:51 lib
drwxr-xr-x 5 root root 4096 Apr 23 06:25 media
drwxr-xr-x 2 root root 4096 Apr 23 06:25 mnt
drwxr-xr-x 2 root root 4096 Apr 23 06:25 opt
dr-xr-xr-x 246 root root 0 Jul 15 20:34 proc
drwx------ 2 root root 4096 Apr 23 06:25 root
drwxr-xr-x 2 root root 4096 Apr 23 06:25 run
drwxr-xr-x 2 root root 4096 Apr 23 06:25 sbin
drwxr-xr-x 2 root root 4096 Apr 23 06:25 srv
dr-xr-xr-x 12 root root 0 Jul 15 20:34 sys
drwxrwxrwt 1 root root 4096 Apr 24 22:51 tmp
drwxr-xr-x 1 root root 4096 Apr 24 22:51 usr
drwxr-xr-x 1 root root 4096 Apr 24 22:51 var
# Run the image with an argument which overrides CMD so it winds up being "ls -r /var" INSIDE the container
$ docker run --rm mybashls -r /var
tmp
spool
run
opt
mail
log
lock
local
lib
empty
cache
# Run the image with an argument which overrides ENTRYPOINT, which then ignores the CMD from the Dockerfile
$ docker run --rm --entrypoint=/bin/echo mybashls
# Run the image with with an argument which overrides ENTRYPOINT plus a custom CMD
$ docker run --rm --entrypoint=/bin/echo mybashls hiya friend
hiya friend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment