Docker Community Forums

Share and learn in the Docker community.

How should I reference different files within my build context?

I am trying to build an image with a dockerfile that looks like this

FROM php:7.2-cli
WORKDIR /usr/src/app
COPY . .
ENTRYPOINT ["php", "./my_script"]

which I build with docker build -t test
and run with docker run test "my_string_arg"

Now, this build happens in a directory with a number of different PHP scripts, of which my_script is only one

Within my_script lies references to other scripts in the directory (via shell_execs)

Each of these other scripts is currently only referenced with a relative path, so a shell exec would look like

shell_exec("./my_other_script $my_string_arg");

My issue is that when I docker run I get errors saying sh: 1: ./my_other_script: not found whereas when I run ./my_script normally in this directory (as in not part of any kind of docker container) it works as intended.

How should I be referencing these scripts?

Can you confirm the other scripts exist inside the container? With the container running, this command will output all the files inside the working folder docker exec test ls.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9ea7e54b9a4a        test                "docker-php-entrypoi…"   3 seconds ago       Up 1 second                             bold_gagarin
me@my_com:/my_workspace$ docker exec 9ea7e54b9a4a ls

They’re all there, and if I write my shell_execs like

shell_exec(__DIR__ . "/my_other_script $my_string_arg");

I still get a similar error

sh: 1: /usr/src/app/my_other_script: not found

The files don’t have an extension? Reference the files including their .php extensions.

They don’t have any extensions, they are PHP scripts but I have them all chmod +x 'ed with the #!/usr/bin/php shebang in the first line

(and thank you for taking the time to help me)

After some debugging, I figured out what’s going on. The issue is that the php path lies at a different directory inside the container than on the host.

If you run shell_exec("which php") inside the container, it returns /usr/local/bin/php. The file has /usr/bin/php at the top which results in this misleading error that the file wasn’t found. While in reality, it couldn’t find the php executable.

There’s several ways to fix this. You can change the shebang in the file, change the files to be .php type files, fix the php path inside the container, etc.

1 Like

That does it, thank you so much, there is 0% chance I would have thought of checking that