Skip to content
Advertisement

ADD multiple Files to a docker but just RUN one of them

It’s just a theoretical question. Is there a way to run the docker but only run one specific script without changing the Dockerfile? Maybe with the docker run [container] command?

Dockerfile:

FROM python:3.8

ADD main1.py
ADD main2.py
ADD main3.py
ADD main4.py
ADD main5.py

Theoretical Command:

docker run docker-test main2.py

Advertisement

Answer

There is nothing “theoretical” about this. Docker copies into place the files, and if they are working executables, you can execute them with docker run image executable … but

  • it requires the files to be properly executable (or you will need to explicitly say docker run image python executable to run a Python script which is not executable)

  • it requires the files to be in your PATH for you to be able to specify their names without a path; or you will need to specify the full path within the container, or perhaps a relative path (./executable if they are in the container’s default working directory)

    docker run image /path/to/executable
    
  • you obviously need the container to contain python in its PATH in order for it to find python; or you will similarly need to specify the full path to the interpreter

    docker run image /usr/bin/python3 /path/to/executable
    

In summary, probably make sure you have chmod +x the script files and that they contain (the moral equivalent of) #!/usr/bin/env python3 (or python if that’s what the binary is called) on their first line.

(And obviously, don’t use DOS line feeds in files you want to be able to execute in a Linux container. Python can cope but the Linux kernel will look for /usr/bin/env python3^M if that’s exactly what it says on the shebang line.)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement