Docker Community Forums

Share and learn in the Docker community.

Different behaviors for dirname at docker run image entrypoint

I am trying to understand the entrypoint behavior of Docker images, specifically related to what is returned by dirname when running executables directly via docker run. (The following is a contrived example, but representative of a real issue I’m trying to work through.)

Using the following Dockerfile to create an image named entrypoint:

FROM ubuntu:18.04
COPY entrypoint.sh /opt/entrypoint/
RUN chmod +x /opt/entrypoint/entrypoint.sh
ENV PATH ${PATH}:/opt/entrypoint
ENTRYPOINT [ "/bin/bash" ]

And the following bash script installed under /opt/entrypoint:

#!/bin/bash
echo "Current directory: `pwd`"
echo "Directory name: `dirname $0`"
echo "Script name: `basename $0`"

When I run entrypoint.sh as an argument to docker run, I unexpectedly get the following:

$ docker run -it entrypoint entrypoint.sh
Current directory: /
Directory name: .
Script name: entrypoint.sh

When I run entrypoint.sh from inside the container, I get the following:

$ docker run -it entrypoint
root@98b127cd16ca:/# entrypoint.sh
Current directory: /
Directory name: /opt/entrypoint
Script name: entrypoint.sh
root@98b127cd16ca:/#

Questions:

  • When I run entrypoint.sh as an argument to docker run, why does dirname return . instead of /opt/entrypoint?
  • What should I be doing differently in the docker run ... entrypoint.sh scenario to get dirname to behave as expected?